YOu can set the attributes of the files you want to keep to readonly and hidden first, delete the rest, and then reset the attributes of the hidden, readonly files back.
attrib +r +s *.bat
del *.*
attrib -r -s *.bat
I used to do that quite often, and wrote a batch file that automated this:
@echo off
@ if "%1" == "%9" goto help
@ if /i %1 EQU ? goto help
@ if /i %1 EQU help goto help
@ attrib +h +s %1
@ %2 %3 /Q
@ attrib -h -s %1
@ goto :EOF
:help
@echo ╔═══════════════════════════════════════════════════════╗
@echo ║ except filespec1 doscommand filespec2 ║
@echo ║ ║
@echo ║ filespec1 The files to exclude from doscommand ║
@echo ║ doscommmand The DOS command to execute on filespec2 ║
@echo ║ filespec2 The files to execute doscommand against ║
@echo ║ ║
@echo ║ Example: ║
@echo ║ ║
@echo ║ except *.txt del *.* ║
@echo ║ ║
@echo ║Deletes all files except text files in the directory ║
@echo ╚═══════════════════════════════════════════════════════╝
It's probably OK just to use the hidden attribute, but I know that del doesn't touch hidden system files, so I set both. Better safe than sorry, IMO.
Based on a comment from Marcus: If you want to extend this to include subdirectories of the current directory, simply change both attrib lines to
attrib <remainder of line> /S
and change the line between the two attrib lines to
@ %2 %3 /Q /S
That should work for most things you'd want except.bat to do.
You can use:
dir /s
If you need the list without all the header/footer information try this:
dir /s /b
(For sure this will work for DOS 6 and later; might have worked prior to that, but I can't recall.)
Best Answer
Have a look at How to find a file in MS-DOS.
See also List of DOS commands