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

Problem deleting subfolders with loop

Status
Not open for further replies.

dburrows1278

Technical User
Jan 25, 2008
13
US
For some reason the loop deleting the subfolders isn't working. If I comment out the IF statement, it runs fine. The file portion runs without a hitch. What am I missing?

Code:
'Setup
	Dim oFSO
	Dim sDirectoryPath
	Dim oFolder
	Dim oDelFolder
	Dim oFileCollection
	Dim oFile
	Dim iDaysOld

'Set the number of days to keep.
	iDaysOld = 1
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	sDirectoryPath = "\\server\share"
	set oFolder = oFSO.GetFolder(sDirectoryPath)
	set oFolderCollection = oFolder.SubFolders
	set oFileCollection = oFolder.Files

'Walks through each file and folder in the archive folder.
'If it is older than the set date, then it is deleted.
	For each oFile in oFileCollection
		If oFile.DateLastModified < (Date() - iDaysOld) Then
			oFile.Delete(True)
		End If
	Next

	For each oDelFolder in oFolderCollection
		If oDelFolder.DateLastModified < (Date() - iDaysOld) Then
			oDelFolder.Delete(True)
		End If
	Next			

'Clean up
	Set oFSO = Nothing
	Set oFolder = Nothing
	Set oFileCollection = Nothing
	Set oFile = Nothing
 
Please, define "isn't working"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
No errors, the subfolders just won't delete. Files will still delete fine.

If I comment out the IF statement like below, the subfolders will then delete... but I need to specify a day or two to keep them around.

Code:
    For each oDelFolder in oFolderCollection
'        If oDelFolder.DateLastModified < (Date() - iDaysOld) Then
            oDelFolder.Delete(True)
'        End If
    Next
 
What is the DateLastModified value of the subfolders you want to delete ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What is the REAL date of modification of the undeleted folder ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry - misunderstood your question.

3 of them right now
02/06/2008
02/05/2008
02/04/2008
 
There shouldn't be mystery around there: only logical conclusion is that the conditional is not met. Such as during the preceeding part of the script, you modified something within those subfolders. The easiest is to check this out yourself, adding this line like this.
[tt]
For each oDelFolder in oFolderCollection
[blue]wscript.echo oDelFolder.path & vbcrlf & oDelFolder.DateLastModified & vbcrlf & (oDelFolder.DateLastModified<(Date()-iDaysOld))[/blue]
If oDelFolder.DateLastModified < (Date() - iDaysOld) Then
oDelFolder.Delete(True)
End If
Next
[/tt]
If you see the condition (the third line in the echo) being met (true) but the subfolder still not being deleted, you can post back to that effect.
 
Also you didn't dim oFolderCollection. It is in principle not a problem if you did not have "option explicit" at the top. But as you said if you comment out the if-end if, subfolders got deleted, I take it there isn't. If there is, the behaviour of the script is not what you described, though, hence a contradiction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top