[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

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
0
ProtocolTech
Asked:
ProtocolTech
1 Solution
 
Gary DewrellSenior Network AdministratorCommented:
Found this answer that shows how to delte directories older than 5 days. You can modify it for your purposes.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21657790.html
0
 
WeistekCommented:
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
0
 
t0t0Commented:
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
ProtocolTechAuthor Commented:
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.
0
 
WeistekCommented:
I tested my original script on a very simple folder structure with no problems.  When I took a copy of a Windows user profile under \Documents and Settings..., I was able to reproduce the error.  Please try substituting line 27 with new code that worked for me...

old line:
      subfolder.delete force

new line:
     fs.deletefolder subfolder.path, True

Regarding your other question, "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."...

Please refer to the following directory structure:
----------------------------------------------------------
G:\
     Dir1 (61 days old)
          SubDir1 (365 days old)
     Dir2 (10 days old)
          SubDir2 (365 days old)

The script will only look at Dir1 and Dir2 (root directories off of the G: drive) and compare their respective ages.  It completely ignores SubDir1 and SubDir2.  It doesn't even care if they exist or how old they are.  In the example above, Dir1 meets your criteria for deletion and Dir1 and all of it's contents including SubDir1 will be deleted.  Dir2 and all of it's contents will be left alone.

Let me know how the code substitution goes to get rid of the permission denied pop-up.
0
 
ProtocolTechAuthor Commented:
YES it works and works well.  Thank you.  This script will be put to good use.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now