VBScript to Delete Old Files
Updated: 10 Dec 2008 | 7 comments
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
Sub folders
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
Here...
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
Hi, And if i want to delete
Hi,
And if i want to delete .log but with a specific name ?
example:
All data with the name pcsxxx.log
xxx is number
I will never understand how
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.
Have Code that may be useful to the thread
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
Dear Sid, If I want to delete
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
This posting is about
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
Would you like to reply?
Login or Register to post your comment.