I recently wrote a script that could be run against a file server's directory tree to generate a list of files over a certain age that we could use for archiving. Everything seems to work just fine, until it hits upon an extremely deep directory, i.e., the full path is longer than 256 characters. At that point it crashes out. I've tried adding On Error Resume Next so that the script would just keep on chugging along, in the hopes that it would just keep going until it got to the next (shorter) directory. Obviously that didn't do the trick. I'm not sure how they actually managed to get that way to begin with, since Windows doesn't let you create directories that deep. Rumor has it that they may have been created by a Mac or UNIX machine.
At any rate, here's the subroutine that does the iterating:
It crashes on the "For Each Item In arrFolders" line about 7 lines into the sub. The error is "Path not found". Does anyone have any idea I can work around this?
I'm suspecting that "Set arrFolders=objFolder.SubFolders" may be collecting the full path for the subfolders, even though they are longer than 256 characters.
At any rate, here's the subroutine that does the iterating:
Code:
Sub CrawlTree(strTargetDir)
Dim objFolder, arrFolders, objFiles, Item, Item2
Set objFolder=objFSO.GetFolder(strTargetDir)
Set arrFolders=objFolder.SubFolders
Set objFiles=objFolder.Files
' Get all sub-folders in this folder
For Each Item In arrFolders
CrawlTree(item)
Next
Item2=0
'Scan through the files collection, find files older than 14 days, and delete.
For Each Item2 in objFiles
Dim strAccessDate, strCreatedate, objFileName, intDaysOld
Set objFileName = objFSO.GetFile(Item2)
strAccessDate = objFileName.DateLastAccessed
intDaysOld = DateDiff("d", strAccessDate, Now)
If intDaysOld > intCutoffAge Then
Wscript.Echo Now & " -- " & objFileName.Path & " is " & intDaysOld & " days old."
Set objFile = objFSO.OpenTextFile(strOutputFile, ForAppending)
objFile.Writeline objFileName.Path
objFile.Close
intFileCount = intFileCount + 1
intFileSize = objFileName.Size
intFileSizeCount = intFileSizeCount + intFileSize
Else
End If
Next
End Sub
It crashes on the "For Each Item In arrFolders" line about 7 lines into the sub. The error is "Path not found". Does anyone have any idea I can work around this?
I'm suspecting that "Set arrFolders=objFolder.SubFolders" may be collecting the full path for the subfolders, even though they are longer than 256 characters.