Is there an MS-DOS command that allows me to delete all files except one?

Consider as an example the following files:


Is there a command to delete all files except a.c?


You can use the for and if commands to accomplish this:

for %i in (*) do if not "%~i" == a.c del "%~i"

This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.

  • +1 never would of thought of using this for a simple delete... Loads of good answers here! – William Hilsum Feb 22 '10 at 19:37
  • 4
    @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :) – Kevin Feb 23 '10 at 2:12
  • 4
    @Mike, you want something like "for %i in (A:\Some\Path\*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command. – Kevin Feb 2 '11 at 16:02
  • 1
    Better use del "%i" – Mugen Oct 20 '16 at 5:06
  • 1
    If you need the IF statement to be case insensitive, change it to IF /I. – jep Sep 20 '18 at 15:11

You could set the file to read only before deleting everything

attrib +r a.c
del *.*
attrib -r a.c
  • And if you care not to see all of the access denied errors just add a > nul 2>&1 to the end of the del line – sonyisda1 Mar 29 at 14:30

No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.

mkdir temp
move a.c temp
erase *.*
move temp\* .
rmdir temp
  • 1
    +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it. – William Hilsum Feb 22 '10 at 19:31
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
  • 1
    what benefit are the [ ] ? – barlop Sep 7 '11 at 1:39
  • 1
    @barlop: %f could have spaces in the filename. – paradroid Sep 7 '11 at 3:12
  • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of []. I could guess at why, but i'd rather hear it from somebody that does it. – barlop Sep 7 '11 at 13:03
  • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would. – paradroid Sep 7 '11 at 13:09
  • @paradroid C:\>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:\>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:\>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes? – barlop Sep 7 '11 at 13:35
FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d
  • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command? – LarsH Feb 10 '16 at 0:19

For speed, I use delen:

delen /! a.c

TCC/LE also has a more powerful del command:

del /[!a.c] *
  • del /[!a.c] * yelds Invalid switch - "[!a.c]". – karlphillip Jan 5 '12 at 20:08
  • @karlphillip: It works fine, "in TCC/LE". – paradroid Jan 6 '12 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.