maheshababu
Technical User
I am creating vbscript to delete files, subfolders more than 5 days old. Following is the code, i get "Delete files and folder older than-5.vbs(39, 1) Microsoft VBScript compilation error: Syntax error" ( error line is "Function DeleteOldFiles(folderName, BeforeDate)" )
Please help
pathfile = "path.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check if the "path.txt" file exists
If Not objFSO.FileExists(pathfile) Then
MsgBox "The file 'path.txt' does not exits", 1, "Error"
WScript.quit 1
End If
'create a stream to read from file
Set instream = objfso.opentextfile(pathfile)
Do While Not instream.atendofstream
folder = instream.readline
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Checks if folder path exists
If Not objFSO.FolderExists(folder) Then
MsgBox folder & " Path not found, Please enter correct path.", 1, "Error"
WScript.quit 1
End If
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
Set startFolder = objFSO.GetFolder(folder)
OlderThanDate = DateAdd("d", -5, Date) ' (adjust as necessary)
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Next
Loop
End Function
thanks
Mahesha
Please help
pathfile = "path.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check if the "path.txt" file exists
If Not objFSO.FileExists(pathfile) Then
MsgBox "The file 'path.txt' does not exits", 1, "Error"
WScript.quit 1
End If
'create a stream to read from file
Set instream = objfso.opentextfile(pathfile)
Do While Not instream.atendofstream
folder = instream.readline
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Checks if folder path exists
If Not objFSO.FolderExists(folder) Then
MsgBox folder & " Path not found, Please enter correct path.", 1, "Error"
WScript.quit 1
End If
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
Set startFolder = objFSO.GetFolder(folder)
OlderThanDate = DateAdd("d", -5, Date) ' (adjust as necessary)
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Next
Loop
End Function
thanks
Mahesha