Link to home
Start Free TrialLog in
Avatar of cunoc
cunoc

asked on

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\
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of cunoc
cunoc

ASKER

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 . . .
>> 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
Avatar of cunoc

ASKER

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 ..
Now I'm confused. Both solutions work (proven), so both should get the points.
Avatar of cunoc

ASKER

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
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.
Avatar of cunoc

ASKER

Hi,
It was my fault, so I would like "to reassign points to Qlemo", please accept my request. Thank you very much.
Avatar of cunoc

ASKER

Hi Qlemo and billprew,
I am sorry for the mess, and hope this will resolved the issue.

Thanks the work you guys provided.