Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript Delete files and folder older than 5 days

Status
Not open for further replies.

maheshababu

Technical User
Jun 30, 2008
4
AP
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
 
I tried removing "Loop", but still the same error

Can someone please help
 
Code:
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

    
'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 startFolder, OlderThanDate

Set startFolder = objFSO.GetFolder(folder)
OlderThanDate = DateAdd("d", -5, Date)  ' (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate
Loop

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = objFSO.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
         objFSO.DeleteFile(file.Path)
      End If
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next

End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark,

It works... Thanks for your quick help... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top