[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-02
12
Medium Priority
?
929 Views
Last Modified: 2012-06-27
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\
0
Comment
Question by:cunoc
  • 5
  • 4
  • 2
11 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 660 total points
ID: 26161795
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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 340 total points
ID: 26161806
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 26161880
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:cunoc
ID: 26161903
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26161939
>> 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
 

Author Comment

by:cunoc
ID: 26161947
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 26161976
Now I'm confused. Both solutions work (proven), so both should get the points.
0
 

Author Comment

by:cunoc
ID: 26162185
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 26162446
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
 

Author Comment

by:cunoc
ID: 26162483
Hi,
It was my fault, so I would like "to reassign points to Qlemo", please accept my request. Thank you very much.
0
 

Author Closing Comment

by:cunoc
ID: 31671959
Hi Qlemo and billprew,
I am sorry for the mess, and hope this will resolved the issue.

Thanks the work you guys provided.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question