32

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

Consider as an example the following files:

a.001  
a.002  
a.003  
a.exe  
a.c  

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

6 Answers 6

43

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.

9
  • +1 never would of thought of using this for a simple delete... Loads of good answers here! Feb 22, 2010 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, 2010 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, 2011 at 16:02
  • 1
    Better use del "%i"
    – Mugen
    Oct 20, 2016 at 5:06
  • 1
    If you need the IF statement to be case insensitive, change it to IF /I.
    – jep
    Sep 20, 2018 at 15:11
21

You could set the file to read only before deleting everything

attrib +r a.c
del *.*
attrib -r a.c
1
  • 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, 2021 at 14:30
11

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
    +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. Feb 22, 2010 at 19:31
3
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
8
  • 1
    what benefit are the [ ] ?
    – barlop
    Sep 7, 2011 at 1:39
  • 1
    @barlop: %f could have spaces in the filename.
    – paradroid
    Sep 7, 2011 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, 2011 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, 2011 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, 2011 at 13:35
1
FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d
1
  • 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, 2016 at 0:19
0

For speed, I use delen:

delen /! a.c

TCC/LE also has a more powerful del command:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .