Batch Script Delete Job

ouch_mybrain_
ouch_mybrain_ used Ask the Experts™
on
Hello Experts,

I would like to know if through a batch script I can delete all folders/subfolders/files in a specified folder But to exclude certain specified folders/subfolders/files?

If someone would be kind enough to direct me to a website or kindly provide the code then that would be much appreciated.

Many Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I would need more details on what you mean by:

"But to exclude certain specified folders/subfolders/files?"

How would these be specified?  Meaning, by full path, or just the name of the folder, subfolder, or file?  Do extensions factor in at all?

The more specific you can be the better a solution you will get here.

~bp
Try it

set match=D:\blah\M*.zip
for %%x in (%match%) do (
del %%x
)
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
So for example, the following folder (Titled "zFolder") has the following subfolders and those subfolders have various subfolders and files:

folder a
folder b
folder c
folder d
folder e
folder f
...
folder z

I want to be able to create a batch file that can delete everything in zFolder but will exclude subfolders "folder a", "folder b", and "folder c". The intention is to run this batch script as an overnight scheduled task. We have an area on our corporate file system that keeps building up with crap and until we find the source of the problem we have to keep doing this manually.

The names of "folder a" "folder b" and "folder c" will not change and therefore the code can statically identify the folder by their names. In the rare case the folder names need to be changed then we would go into the code and change them.

Hope that helps clarify billprew.

Thanks.

Author

Commented:
Thanks Manoj I will give that a go and will let you know the outcome.
Test your restores, not your backups...
Top Expert 2016
Commented:
Here's a simple example of how you could do what you described in a BAT script.  Right now it will only ECHO the RD commands it would do, so that you can test it to make sure it skips the ones you want to preserve.  If it looks good remove the ECHO before the RD command.
@echo off
setlocal EnableDelayedExpansion

set BaseDir=c:\zFolder
set ExclDirs="folder a","folder b","folder c"

for /D %%A in ("%BaseDir%\*.*") do (
  set DeleteMe=Y
  for %%B in (%ExclDirs%) do (
    if "%%~nA" EQU "%%~B" set DeleteMe=N
  )
  if !DeleteMe! EQU Y ECHO rd /S /Q "%%~A"
)

Open in new window

~bp

Author

Commented:
Hi Manoj, sorry it didn't work - I think it's more to do with how I executed it though.

Author

Commented:
Thanks...this worked. This is what the script finally looked like:

@ECHO OFF
setlocal EnableDelayedExpansion

set BaseDir=C:\Documents and Settings
set ExclDirs="Administrator","user.name1","user.name2","user.name3"

for /D %%A in ("%BaseDir%\*.*") do (
  set DeleteMe=Y
  for %%B in (%ExclDirs%) do (
    if "%%~A" EQU "%BaseDir%\%%~B" set DeleteMe=N
  )
  if !DeleteMe! EQU Y rd /S /Q "%%~A"
)
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Great, glad that was helpful, thanks.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial