Solved

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

Posted on 2016-11-10
11
84 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 54

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 54

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 54

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
 
LVL 54

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 54

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 54

Expert Comment

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

~bp
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
What to do when Windows Update is not working correctly? What tools can I use to detect the cause of the malfunction problem? What does this numeric error code mean? These and other questions that you have been asking in the past are answered here (…
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 process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

730 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