VBScript to Delete Old Files
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

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
Would you like to reply?
Login or Register to post your comment.