Solved

Windows 2012 Script / Batch to delete 1 year old files EXCEPT FOR files within a directory

Posted on 2016-11-10
11
28 Views
Last Modified: 2016-11-10
Is there a way to use a batch file / script to delete files and / or folders from a file server EXCEPT if the files are within a particular folder:

E.g.   Folder structure is like:

E:\Storage\
E:\Storage\Accounts
E:\Storage\Accounts\Scans\Receipts
E:\Storage\Accounts\Scans\Receipts\Old
E:\Storage\Accounts\Junk
E:\Storage\Accounts\Junk
E:\Storage\Accounts\Scans\Invoices
E:\Storage\Accounts\Scans\Invoices\2014
E:\Storage\Accounts\Scans\Invoices\2015
E:\Storage\Accounts\Scans\Invoices\2016


Example,  How to delete folder older that 365 days would be :
FORFILES /S /D -365 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

However, how to I put a EXCEPTION option to exclude E:\Storage\Accounts\Scans\Invoices and all subfolders and files?

Thanks in advance!
0
Comment
Question by:Mchallinor
  • 6
  • 5
11 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41882183
Okay, here is a way to approach this.  Basically we use two scripts, the first which executes the FORFILES command and for all folders fould meeting the age requirement we call a second script to check for the excluded folder and only remove the ones we want.  Below are the two scripts I used with some comments, take a look and see if this makes sense.

***** FIRST SCRIPT (EE28982238A) *****
@echo off
setlocal

rem Define base folder to scan folder for removal in
set BaseDir=E:\Storage\Accounts\Scans\Invoices

rem Define fully qualified path to second script (in same place as this script)
set CheckScript=%~dp0
set CheckScript=%CheckScript%EE28982238b.bat

rem Find the folders to check for removal, call second script to exclude certain folders
FORFILES /P "%BaseDir%" /S /D -365 /C "cmd /c IF @isdir == TRUE call %CheckScript% @path"

Open in new window

***** SECOND SCRIPT (EE28982238B) *****
@echo off
setlocal EnableDelayedExpansion

rem Define protected folder root to never remove
set ExcludeDir=E:\Storage\Accounts\Scans\Invoices

rem Get folder being checked from command line
set CurrentDir=%~1

rem Try to replace current folder path with excluded folder path to see if we exclude it
set TestDir=!CurrentDir:%ExcludeDir%=!

rem If excluded folder path was not in current folder path then okay to delete
if "%TestDir%" EQU "%CurrentDir%" (
  rd /s /q "%CurrentDir%"
)

Open in new window

~bp
0
 

Author Comment

by:Mchallinor
ID: 41882218
Hey Bill,  That's a really good solution!  Thanks!

However I've made a slight error in my ways,  actually I need to be deleting the FILES older than 365 days but do not delete any files within the "E:\Storage\Accounts\Scans\Invoices " folder and subfolders.

Does that require a totally different script or can I modify what you have sent me?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41882258
Let me do a little testing here on a modification to that script and see if we can easily get that with an adjustment.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41882289
Okay, fairly small adjustments, this seems to work here, give a test or toss back additional questions etc.

***** FIRST SCRIPT (EE28982238A) *****
@echo off
setlocal

rem Define base folder to scan folder for removal in
set BaseDir=E:\Storage\Accounts\Scans\Invoices

rem Define fully qualified path to second script (in same place as this script)
set CheckScript=%~dp0
set CheckScript=%CheckScript%EE28982238b.bat

rem Find the folders to check for removal, call second script to exclude certain folders
FORFILES /P "%BaseDir%" /S /D -365 /C "cmd /c IF @isdir == FALSE call %CheckScript% @path"

Open in new window

***** SECOND SCRIPT (EE28982238B) *****
@echo off
setlocal EnableDelayedExpansion

rem Define protected folder root to never remove
set ExcludeDir=E:\Storage\Accounts\Scans\Invoices

rem Get folder being checked from command line
set CurrentFile=%~1

rem Try to replace current folder path with excluded folder path to see if we exclude it
set TestFile=!CurrentFile:%ExcludeDir%=!

rem If excluded folder path was not in current folder path then okay to delete
if "%TestFile%" EQU "%CurrentFile%" (
  del "%CurrentFile%"
)

Open in new window

~bp
1
 

Author Comment

by:Mchallinor
ID: 41882410
Hi Bill,  That's looking pretty awesome. Thanks again!

So what if I want to exclude 2 directories :   lets say

set ExcludeDir=E:\Storage\Accounts\Scans\Invoices  
and
set ExcludeDir=E:\Storage\Accounts\Scans\Purchases  


How could we make a list of exclusions?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:Bill Prew
ID: 41882432
How many are we talking about, just a few, or a long list?

~bp
0
 

Author Comment

by:Mchallinor
ID: 41882441
Just a short list.  Maybe 5 directories maximum.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41882474
Okay, added a list where you can specify the exception folders.  Now, depending on how many files you have, how many are in these exception folders, and meet the old age criteria, this may start to get a bit slow, but not sure how large your folders are and how many will be purged at a time, etc.  Give it a try.

***** FIRST SCRIPT (EE28982238A) *****
@echo off
setlocal

rem Define base folder to scan folder for removal in
set BaseDir=E:\Storage\Accounts\Scans\Invoices

rem Define fully qualified path to second script (in same place as this script)
set CheckScript=%~dp0
set CheckScript=%CheckScript%EE28982238b.bat

rem Find the folders to check for removal, call second script to exclude certain folders
FORFILES /P "%BaseDir%" /S /D -365 /C "cmd /c IF @isdir == FALSE call %CheckScript% @path"

Open in new window

***** SECOND SCRIPT (EE28982238B) *****
@echo off
setlocal EnableDelayedExpansion

rem Define protected folder root to never remove
set ExcludeDirs="E:\Storage\Accounts\Scans\Invoices","E:\Storage\Accounts\Junk"

rem Get folder being checked from command line
set CurrentFile=%~1

rem Check list of exception folders and do not delete file if in any of those
set DeleteFlag=TRUE
for %%A in (%ExcludeDirs%) do (
  rem Try to replace current folder path with excluded folder path to see if we exclude it
  set TestFile=!CurrentFile:%%~A=!

  rem If excluded folder path was not in current folder path then okay to delete
  if "!TestFile!" NEQ "%CurrentFile%" (
    set DeleteFlag=FALSE
    goto :CheckFlag
  )
)

:CheckFlag
  rem Check if the file is okay to delete
  if "%DeleteFlag%" EQU "TRUE" (
    del "%CurrentFile%"
  )

Open in new window

~bp
0
 

Author Comment

by:Mchallinor
ID: 41882508
Hi, Bill

You legend.  That's working,  yes a bit of a slower process, but fine for the task I'm doing.

Great help!
0
 

Author Closing Comment

by:Mchallinor
ID: 41882510
Nice work!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41882553
Great, glad that was useful, thanks for the feedback.

~bp
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Understanding the various editions available is vital when you decide to purchase Windows Server 2012. You need to have a basic understanding of the features and limitations in each edition in order to make a well-informed decision that best suits y…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now