Link to home
Start Free TrialLog in
Avatar of ProtocolTech
ProtocolTech

asked on

Need batch to delete folders by age

Need:
Hello, I am looking for a "simple" batch file that will delete any folder in the root of the drive, older than "x" days - but not the subfolders.

Setting:
My client insists on saving her data onto a large USB drive. I schedule a batch file to run similar to the one below:

          md "G:\%DATE%"
          robocopy /mir C:\Data "G:\%DATE%\data"

This runs each week and puts a copy of "data" into a folder under G:\ named after the date it runs.
          i.e. "G:\Fri 04-24-2009\data"

Problem:
In 12 weeks the drive is full. I need a script that will delete any folder - in the root - created more than 60 days ago (8 weeks). I do not want to delete sub folders older than 8 weeks due to most of the data in the sub folders are themselves older that 90 days.  This allows the client to have 2 months worth of backups at one week increments.

Request:
Can this be done via the command line?  What I am looking for is a line I can add to a batch file, or a Resource Tool Kit tool, that I could use for many clients.    I know a VB Script would work, unfortunately my strength is with batch files not VB .  If all you have is a VB script Id gratefully accept that too.

TIA
Avatar of Gary Dewrell
Gary Dewrell
Flag of United States of America image

Found this answer that shows how to delte directories older than 5 days. You can modify it for your purposes.

https://www.experts-exchange.com/questions/21657790/Need-SIMPLE-DOS-Delete-Folder-older-than-5-days.html
Avatar of Weistek
Weistek

You can call the vbscript from within a batch file.  This one has a safety to ensure that you are working with the root folder of the logical drive.

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

' set path to root of logical drive
rootfolderpath = "G:\"
' set number of days for threshold to purge folder
purgeafter = 60

set folder = fs.GetFolder(rootfolderpath)
''Add single quote before next line to supress pop-up message
wscript.echo folder.path

' check to ensure this is a root folder...otherwise exit
if folder.isrootfolder=true then

      For each item in folder.SubFolders

            set subfolder = fs.getfolder(item.path)

            ' comparing age based on days since created date (not modified or accessed)
            age =  datediff("d",subfolder.datecreated,date)

            ' if age is greater than threshold number of days... force delete
            if age > purgeafter then
''Add single quote before next line to supress pop-up message
wscript.echo subfolder.path & " is " & age & " days old.  purging..."
                  subfolder.delete force
            end if


      Next


else
      ' warning message if not working with root folder
''Add single quote before next line to supress pop-up message
      wscript.echo folder.path & " is not a root folder."

end if

set subfolder = Nothing
set folder = Nothing
Set fs = Nothing
Avatar of ProtocolTech

ASKER

Weistek:
Thank you for your script.   I tried it and it appears to be what I need though I get an WSH Error dialogue box appear stating an error in:
line: 27
Char: 19
Error: Permission Denied.

I am not sure which folder this is as I can delete all from windows.  I also added everyone FC Share and NTFS to try and drop the permissions.  Not sure why this error occurs.

Also does this only delete the Folder in the root?  I do no wish it to delete the subfolders of Folders it doesn't delete.


t0t0:
I have used that application in the past and hadn't thought of using it for this task.  I'll look into it.

gdewrell:
The xcopy /d requires a specified date, if I read that correct.  I'd rather not have to tweak the specific date each week.  Now the complex code.... I'd rather no edit the VB.  My strength is in Network Infrastructure and Servers, sadly not scripting.

Thanks for all your contributions.
ASKER CERTIFIED SOLUTION
Avatar of Weistek
Weistek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YES it works and works well.  Thank you.  This script will be put to good use.