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?

Gareth
  • 18,809
nunos
  • 641

6 Answers6

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.

Kevin
  • 3,846
  • 1
  • 18
  • 9
  • +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 02: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
  • 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 02 '11 at 16:02
  • 1
    Better use del "%i" – Mugen Oct 20 '16 at 05:06
  • As far as I know, there is no or operator for if in batch. So how would you delete all files except two (or three, four, ...) ? – Munchkin Mar 10 '17 at 16:49
  • @Munchkin, see http://stackoverflow.com/q/8438511/91756 for some ways to simulate an or statement. – Kevin Mar 11 '17 at 03:54
  • 1
    If you need the IF statement to be case insensitive, change it to IF /I. – jep Sep 20 '18 at 15:11
  • I had to enclose a.c in double quotes for the equality check to pass for %i in (*) do if not "%~i" == "a.c" del "%~i" – rgvlee Mar 09 '22 at 06:33
21

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 '21 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 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
3
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
  • 1
    what benefit are the [ ] ? – barlop Sep 07 '11 at 01:39
  • 1
    @barlop: %f could have spaces in the filename. – paradroid Sep 07 '11 at 03: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 07 '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 07 '11 at 13:09
  • @paradroid C:>if [4 r]==[4 r] echo abc 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 07 '11 at 13:35
  • @paradroid C:>if [%USERPROFILE%]==[%USERPROFILE%] echo abc and was unexpected at this time. <-- [] make no difference. Also even without the brackets, %f works look C:>for /f "tokens=*" %f in ("a b") do IF %f==%f echo same – barlop Nov 23 '11 at 14:36
  • @paradroid so brackets never have any effect on spaces. and %f in an IF just works with spaces. – barlop Nov 23 '11 at 14:37
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
Darth
  • 11
  • 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 00:19
0

For speed, I use delen:

delen /! a.c

TCC/LE also has a more powerful del command:

del /[!a.c] *
paradroid
  • 22,992