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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not deleting empty folders

Status
Not open for further replies.

dheerajreddy926

Programmer
Apr 15, 2012
10
EU
Hi All,

The following script is deleting only files in LOGARCHIVE folder leaving the empty folder .I want to delete empty folders even. How can i correct it. More over i get an error when it goes to 2nd path \server\lodarchive sayng path not found .Can people suggest me .

In test3.txt i gave both servers one windows and one AIX .Its failing at AIX server sayn path not found

..................................................................
pathfile = "d:\test3.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Check if the "test2.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
Dim msg

msg = "\\" & folder & "\Siebel\enterprises\siebel_aix\" & folder & "\logarchive"


'Checks if folder path exists

If Not objFSO.FolderExists(msg) Then

msg = "\\" & folder & "\d$\sea77\siebsrvr\LOGARCHIVE"


MsgBox folder & " Path not found, Please enter correct path.", 1, "Error"
WScript.quit 1

End If

Dim startFolder, OlderThanDate

Set startFolder = objFSO.GetFolder(msg)
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
 
the deleteOldFiles function doesn't delete the object folder, just the child objects.

add a line
Code:
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
    DeleteOldFiles subFolder.Path, BeforeDate
    [red]objFSO.deleteFolder subFolder.Path[/red]
Next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I could resolve one issue ie.. it now takes windows path also ..

Now i only need how to delte empty folders after delting files inside it ..plz help :)
 
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
objFSO.deleteFolder subFolder.Path
Next
-----------
The above code deletes all folders,i need to delete folders and files which are 5 days old.Plz help
 
Check to see if the folder contains files or folders after it's been processed. If it doesn't, it's safe to say that it should be deleted.

Code:
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
    DeleteOldFiles subFolder.Path, BeforeDate
    [red]if ((subFolder.files.count = 0) AND (subFolder.subFolders.count = 0)) then[/red]
       subFolder.delete 
    [red]end if[/red]
next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top