Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 22 '10 at 19:19

This question came from our site for professional and enthusiast programmers.

6 Answers 6

up vote 18 down vote accepted

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.

share|improve this answer
+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
@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
How can this be modified so it uses another directory besides the current directory? I tried changing the * to a path that ended with ., but no joy. –  Mike Cole Jan 31 '11 at 15:46
@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

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
share|improve this answer
+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

You could set the file to read only before deleting everything

attrib +r a.c
del *.*
attrib -r a.c
share|improve this answer
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
share|improve this answer
what benefit are the [ ] ? –  barlop Sep 7 '11 at 1:39
@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
share|improve this answer

For speed, I use delen:

delen /! a.c

TCC/LE also has a more powerful del command:

del /[!a.c] *
share|improve this answer
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
Aha! Didn't see that comment before, thanks bro. –  karlphillip Jan 6 '12 at 11:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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