Jump to content

[Question] - Batch Deletion of Files older than 30 days - Help Please


amd64lover

Recommended Posts

I have a folder of backups. Each backup is a .rar file. To conserve space, i would like a batch program that deletes all the files (there are only rar files in there) in the Backup folder that are older than 30 days. Does anyone know how to do this? I would prefer it to be a .bat file so i wouldnt have to install any extra software and could set it to run automatically using task scheduler... Thanks!

Title Edited - Please follow new posting rules from now on.

--Zxian

Link to comment
Share on other sites


Be careful with this - there's no error checking. It'll go into a directory and delete all files that are older than 30 days without warning!

Option Explicit
on error resume next
Dim objFSO
Dim sDirectoryPath
Dim objFolder
Dim objFileCollection
Dim objFile
Dim iDaysOld

'Set the number of days to go back
iDaysOld = 30
Set objFSO = CreateObject("Scripting.FileSystemObject")

'The sDirectoryPath can be a local folder or a \\server\share
sDirectoryPath = "\\MyServer\MyFolder"
set objFolder = objFSO.GetFolder(sDirectoryPath)
set objFileCollection = objFolder.Files

'If any file is older than iDaysOld value, then delete it.
For each objFile in objFileCollection
If objFile.DateLastModified < (Date() - iDaysOld) Then
objFile.Delete(True)
End If
Next

'Finish up
Set objFSO = Nothing
Set objFolder = Nothing
Set objFileCollection = Nothing
Set objFile = Nothing

Link to comment
Share on other sites

Perhaps this is a first step: This command lists the date of the file and the file names. No you just have to add the check for the date it delete the file or folder.

In the variable %i the date is contained and in the vatiable %l the filename, %j contains the time of the fileceation and %k contains either "<DIR>", if it's a directory, or the filesize.

for /f "tokens=1,2,3*" %i in ('dir /OD') DO echo %i %l

If you're using this in a batch, you have to set double '%' instead of one.

Link to comment
Share on other sites

thanks to both of you!

@cluberti

is that a batch file or vbs file? do i have to make a file for each folder that i wanted cleaned of files that are older than 30 days? its not a big deal, i only plan on using it for one folder anyways.

Link to comment
Share on other sites

@cluberti

great thanks!

@jftuga

i was checking out delen and was using it to delete the contents of my backup folder (C:\Buildsof\Backups), but i have yet to figuer out a way to do this without deleting the folder itself. The command i am using is 'c:\buildsof\backups\ /sxy' .... i thought the last trailing backslash after the directory location would help, but it didnt.... any ideas?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...