Pong, in this code, fill the "Keep" variable with the needed filenames.
I took for granted that those files cound be anywhere so in the test I really check the name+extension only, not the path, so they could be inany sub-path.
EDIT: Ok, there goes the revised more deeply tested version for Pong. Sorry for posting too hastily, wanted to help but guess I didn't test my script enough.
Although this time, allows me to show an example or recursive programming (function :CleanDirs calling itself) in XP batch programming :)
Cleanup.cmd:
@echo off
set Keep="%~nx0" "file1.ext" "file2.ext"
set StartDir=%~dp0
call :CleanDirs "%StartDir:~0,-1%"
exit /b 0
:CleanDirs
call :CleanFiles "%~1"
for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do call :CleanDirs "%~1\%%~d"
if exist "%~1" rmdir /Q "%~1">nul 2>nul
exit /b 0
:CleanFiles
for /f "delims=" %%f in ('dir /a-d /b "%~1"2^>nul') do (
set _keep=
for %%k in (%Keep%) do if /i "%%~nxf"=="%%~k" set _keep=yes
if not defined _keep del /f "%~1\%%~f" 2>nul
)
exit /b 0
Still, hope this helps even more.
Notes
set Keep= : Adjust with any filename you need to keep. The script will keep the subfolders holding those files.
set StartDir=%~dp0 : Will ajust itself to whereever the script is located. If you want to move the script elsewhere, you could call it with a directory name as argument and change assignation here as ="%~1"
Coding tips
%~1 : (Tild) remove enclosing quotes. Will not remove ALL quotes, only one leading/Trailing.
%~nx0 : (tild n x)gets the file**n**ame+e**x**t of the batchfile. Warning, this will change if this is put into a function. Adjust as needed.
%~dp0 : (tild d p) Gets the drive/full path (including trailing baskslash (\) of the batchfile. Warning, this will change if this is put into a function. Adjust as needed.
%StartDir:~0,-1% : (Tild Start,Stop) Gets a substring of the variable, here from position 0 to last character minus one position (used to strip trailing \).