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?
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.
You could set the file to read only before deleting everything
attrib +r a.c
del *.*
attrib -r a.c
> nul 2>&1 to the end of the del line
– sonyisda1
Mar 29 '21 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
FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f
FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d
%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
forregularly you come up with all kinds of crazy scenarios for it. :) – Kevin Feb 23 '10 at 02:12del "%i"– Mugen Oct 20 '16 at 05:06oroperator forifin batch. So how would you delete all files except two (or three, four, ...) ? – Munchkin Mar 10 '17 at 16:49IFstatement to be case insensitive, change it toIF /I. – jep Sep 20 '18 at 15:11a.cin double quotes for the equality check to passfor %i in (*) do if not "%~i" == "a.c" del "%~i"– rgvlee Mar 09 '22 at 06:33