Video Screencast Help

VBScript to Delete Old Files

Created: 10 Dec 2008 • Updated: 10 Dec 2008 | 8 comments
Sidd's picture
+13 13 Votes
Login to vote

Here is a script to delete old files (let's say 15 day old files.) This script will help in clearing old files and some run time generated log files which may increase in size (MB) over a period of time.

This script can either be used with Scheduled within the Normal Scheduled Tasks or Deployment Server Script job.

Changes that need to be made while using this script in your environment are:

  • Specify Directory Path from where you want to clear the old files in the following line of code in the main script.

    sDirectoryPath = "C:\MyFolder"

  • Specify Number of days old file to delete in the following Line of code in the main script.

    iDaysOld = 15

  • Specify the extension of file that you want to delete and the number with number of character in the file extension in the following Line of code in the main script.

    If LCase(Right(Cstr(oFile.Name), 3)) = "txt" Then

'************ Start of Code **********************

Option Explicit 
On Error Resume Next 
Dim oFSO, oFolder, sDirectoryPath 
Dim oFileCollection, oFile, sDir 
Dim iDaysOld 

' Specify Directory Path From Where You want to clear the old files 

sDirectoryPath = "C:\MyFolder" 

' Specify Number of Days Old File to Delete

iDaysOld = 15

Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(sDirectoryPath) 
Set oFileCollection = oFolder.Files 

For each oFile in oFileCollection

'This section will filter the log file as I have used for for test case 
'Specify the Extension of file that you want to delete 
'and the number with Number of character in the file extension 
	
	If LCase(Right(Cstr(oFile.Name), 3)) = "log" Then
 	
 		If oFile.DateLastModified < (Date() - iDaysOld) Then 
 		oFile.Delete(True) 
 		End If 
 
	End If   
Next 

Set oFSO = Nothing 
Set oFolder = Nothing 
Set oFileCollection = Nothing 
Set oFile = Nothing 

'**************** End of Code *******************

Thanks,
Sid

Download Filed Under:

Comments 8 CommentsJump to latest comment

Antonp's picture

Hi Sid,

Your script, does it only delete the older files in that folder or does it check all the sub folders within that directory.

And another thing, if i have more than one folder to check for older files, would i need to copy the script and modify the path or can we add another line to this script for the paths?

Antonp 

Technical Consultant
Http://www.alttech.co.za

+1
Login to vote
Sidd's picture

Sorry for the late reply...
This Script will delete the older files that are there in that folder itself. here it is 
sDirectoryPath = "C:\MyFolder" . it will not check for all the sub folders within that directory.

If you have more folders to check in that case.
1. if subfolder are there in that case go for little modification in the script is required.
2. if it is different directory in that case need to define the pathe of the folder
   otherwise if you have multiple directory then, as a function you can use the above code call the function by defining the path.

Thanks
Sid

+11
Login to vote
vincent.monbaron's picture

Hi,

And if i want to delete .log but with a specific name ? 
example:

All data with the name pcsxxx.log
xxx is number

0
Login to vote
rays_30's picture

I will never understand how these scripts work, but you have been pretty explicit so i hope i will manage writing a correct code this time. it's not the first time i have trouble with informatics, few months back i had problems with some dll files, i had no idea how to use them, i had to make a dll search and learn more about them before being able to open them.

0
Login to vote
jmazzella's picture

I think I have written a VBScript application that does alot of similar things :) The code is zipped up and can be downloaded via the linked to my page.The link also contains some sample code and detals about all it does.

http://www.code-bytes.com/Automate_WSH_VBScript_To...

* The code that I have allows you to Delete and/or archive files by specifying ...
1.)directory to be searched
2.) age of the file
3.) Use regular expressions to allow for pattern matching file names.

The script is highly configurable and creates a log of actions taken on files matching your criteria.

 The code is free, but if it really helps you please feel free to donate:)

John

0
Login to vote
trieuttc's picture

Dear Sid,

If I want to delete some old log files which were specified the day of previous month, then a new log file will be overwritten on the old log file.

For example:

Today is May 07th, 2011. After that I want to delete the log file of the April 07th, 2011 for overwrite log file of May 07th, 2011. 

Next, The day 08th of May will be overwritten on the log file of the April 08th...And do it until the day 30th of month...

Could you please help me to do that? I'm deeply grateful for your help!

Best regards,

Thanh Trieu

0
Login to vote
JonClone's picture

This posting is about VBScript to delete old files. It is good that such a posting has been done. I think many would like to know about such a posting. I like Groupon web site and I think more clones are emerging.

Jon Clone - Groupon Clone

-1
Login to vote
Rob21's picture

Sid:

Your "Delete Old Files" script is great. I would like to use it please. But can you wrap some logging around the script to produce an output <.txt> log file please. Would like to see log capture of what the script is actually performing/deleting; i.e. script execution date/time, filename of deleted file, date/time stamp of file being deleted and source path of file deleted.

Thx,

Rob21

0
Login to vote