Question : Batch file Delete all files from a directory, except the 2 files ..

Hi,

These files are currently in C:\WINDOWS\Web\Wallpaper0001

Pic0001.JPG
FUN000w.GIG
WHY0001.BMP
Do_Not_Delete_Me.txt
Do_Not_Remove_Me.txt

What I want to do is to Delete all files in C:\WINDOWS\Web\Wallpaper0001

Pic0001.JPG
FUN000w.GIG
WHY0001.BMP

Except two files below, I don't want to delele ..

Do_Not_Delete_Me.txt
Do_Not_Remove_Me.DOC

Thanks

:://////////////////////////////////////////////////
::
:: Any other methods, that more better than the CODE below ... and
:: When it done the job, it should ECHO me that how many files has
:: been deleted from that directory and what files have left.
::
:://////////////////////////////////////////////////
 
@ECHO OFF

MD C:\WINDOWS\Web\Wallpaper0002

XCOPY C:\WINDOWS\Web\Wallpaper0001\*.* C:\WINDOWS\Web\Wallpaper0002\

ECHO Y | DEL C:\WINDOWS\Web\Wallpaper0001\*.*

COPY C:\WINDOWS\Web\Wallpaper0002\Do_Not_Delete_Me.txt C:\WINDOWS\Web\Wallpaper0001\
COPY C:\WINDOWS\Web\Wallpaper0002\Do_Not_Delete_Me.DOC C:\WINDOWS\Web\Wallpaper0001\

RD C:\WINDOWS\Web\Wallpaper0002\

Answer : Batch file Delete all files from a directory, except the 2 files ..

A similar method: Move only those two files, delete everything, move the files back:
@ECHO OFF
cd /D C:\WINDOWS\Web
MD Wallpaper0002 2>nul
for %%F in ("Do_Not_Delete.Me.txt" "Do_Not_Remove_Me.Txt") do move "Wallpaper0001\%%~F" "Wallpaper0002\"
del "Wallpaper0001\*" /f /q
move "Wallpaper0002\*" "Wallpaper0001\"
rd "Wallpaper0002" /q

That one is more flexible, as you have to provide the file names only once (in the FOR). The double quotes are not necessary here, but as soon as there are spaces in paths or file names, they are required.

Another popular approach is to set those files to e.g. hidden/system, and delete anything non-hidden:

@ECHO OFF
cd /D C:\WINDOWS\Web\Wallpaper0001
for %%F in ("Do_Not_Delete.Me.txt" "Do_Not_Remove_Me.Txt") do attrib +h %%F
del /a:-h * /q /f
attrib -h *



Random Solutions  
 
programming4us programming4us