?
Solved

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

Posted on 2016-11-10
11
Medium Priority
?
181 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 57

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 57

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 57

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 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

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

~bp
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…
Suggested Courses

765 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