Solved

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

Posted on 2016-11-10
11
51 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 52

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 52

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 52

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 52

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
OfficeMate Freezes on login or does not load after login credentials are input.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…

863 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

18 Experts available now in Live!

Get 1:1 Help Now