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\
cunocAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A similar method: Move only those two files, delete everything, move the files back:
@ECHO OFFcd /D C:\WINDOWS\WebMD Wallpaper0002 2>nulfor %%F in ("Do_Not_Delete.Me.txt" "Do_Not_Remove_Me.Txt") do move "Wallpaper0001\%%~F" "Wallpaper0002\"del "Wallpaper0001\*" /f /qmove "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\Wallpaper0001for %%F in ("Do_Not_Delete.Me.txt" "Do_Not_Remove_Me.Txt") do attrib +h %%Fdel /a:-h * /q /fattrib -h *


0
 
Bill PrewCommented:
Could just do the deletes one file at a time and skip over the ones to be preserved.  Like attached.  I left the delete command as an ECHO for testing, so you can make sure it does what you want.  If it works just remove the ECHO from before the DEL.

~bp
@echo off
set BaseDir=c:\windows\web\wallpaper0001
for /f "tokens=*" %%A in ('dir /a-d /b "%BaseDir%"') do (
  if not "%%A" == "Do_Not_Delete_Me.txt" (
    if not "%%A" == "Do_Not_Remove_Me.txt" (
      ECHO del /q "%%~fA"
    )
  )
)

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If doing it like that (not recommended if many files are in the folder), I would modify the code for flexibility to something like:

@echo off
set BaseDir=c:\windows\web\wallpaper0001
for /f "tokens=*" %%A in ('dir /a-d /b "%BaseDir%"') do (
  echo %%A | findstr "Do_Not_Delete_Me.txt Do_Not_Remove_Me.txt" >nul || (
      ECHO del /q "%%~fA"  
  )
)

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
cunocAuthor Commented:
Hi billprew:,
Here is errors I got when run the code..

@echo off
set BaseDir=c:\windows\web\wallpaper0001

for /f "tokens=*" %%A in ('dir /a-d /b "%BaseDir%"') do (
  if not "%%A" == "Do_Not_Delete_Me_0001.txt" (
    if not "%%A" == "Do_Not_Remove_Me_0002.txt" (
      del /q "%%~fA"
    )
  )
)

PAUSE ..
/////////////////////////////////////

Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Ascent.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Autumn.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Azul.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Bliss.bmp
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Crystal.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Follow.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Friend.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Home.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Moon flower.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Peace.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Power.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Purple flower.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Radiance.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Red moon desert.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Ripple.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Stonehenge.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Tulips.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\TXT, Do_Not_Delete_Me_0001.t
xt
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\TXT, Do_Not_Delete_Me_0002.t
xt
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Vortec space.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Wind.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\Windows XP.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\WinS_DW_31.jpg
Could Not Find N:\USB, BACKUP, 0001\DOC0001\BAT0001\WinS_DW_64.jpg
Press any key to continue . . .
0
 
Bill PrewCommented:
>> cunoc

That's rather strange.

What did you name the BAT file?

And do you have a mapped drive called N:, and does it have a folder "USB, BACKUP, 0001\DOC0001\BAT0001" in it?

~bp
0
 
cunocAuthor Commented:
Hi Qlemo:

It works ... thanks

@ECHO OFF

cd /D C:\WINDOWS\Web\Wallpaper0001
for %%F in ("Do_Not_Delete_Me_0001.txt" "Do_Not_Delete_Me_0002.txt") do attrib +h %%F
del /a:-h * /q /f
attrib -h *


PAUSE ..
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Now I'm confused. Both solutions work (proven), so both should get the points.
0
 
cunocAuthor Commented:
Hi Qlemo:
Please accept my mistake, I was in a hurry and not pay attention, so how i am going to give the score to you as your work.. Please let me know,

Thanks
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Request Attention (red exclamation mark), with a reason like "want to reassign points" - that will create a request in Community Support. A Moderator will handle that by reopening the question ASAP, then you can close again.
0
 
cunocAuthor Commented:
Hi,
It was my fault, so I would like "to reassign points to Qlemo", please accept my request. Thank you very much.
0
 
cunocAuthor Commented:
Hi Qlemo and billprew,
I am sorry for the mess, and hope this will resolved the issue.

Thanks the work you guys provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.