How to delete *.* excluding some extensions?

前端 未结 6 2144
夕颜
夕颜 2020-12-10 05:46

I\'m trying to make a batch file on Windows for deleting all the files in the current directory but excluding 4 file extensions (log, sdb, SDK, bat).

I have tried th

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 06:41

    I ran across this topic searching for a way to delete hundres of files created by a virus. Non of the solutions really worked for me, so I figured out how to do it from a command line. I needed only to keep 2 extensions (mail archive). This did the trick:

    for /R %f in (*) do if not %~xf==.ex1 if not %~xf==*.ex2 del "%f"
    

    I use the /R to work recursive: look in all subfolders. The %~xf looks at the extension only (for some reason it didn't work without it). I use the quotes "%f" at the delete command to cover the windows long names with spaces (especially in folder names). Also for some reason, adding spaces before and behing the == gave errors.

提交回复
热议问题