Solved

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

Posted on 2016-11-10
11
59 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 53

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 53

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
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.

 
LVL 53

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 53

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 53

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 53

Expert Comment

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

~bp
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Recently, I was assigned the task of performing a hardware refresh in the datacenter. The previous Windows 2008 systems were connected to the SAN via fiber channel HBA’s and among other thing, had PowerPath installed in order to provide sufficient f…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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 use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…

803 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