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
  • 19,080
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
  • 4,011
  • 1
  • 19
  • 9
21

You could set the file to read only before deleting everything

attrib +r a.c
del *.*
attrib -r a.c
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
3
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
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
  • 1
0

For speed, I use delen:

delen /! a.c

TCC/LE also has a more powerful del command:

del /[!a.c] *
paradroid
  • 23,297