I receive a permission denied error on line 34, character 10 when I run this script against a folder location that has multiple nested folder levels and lots of files. I am running the script against the root folder. I have gone back through and reapplied permissions to every single file and folder for 'Everyone'. I even made a copy of the entire folder structure, then manually deleted the files and folders using Windows explorer. This confirmed that there was not a permission issue with accessing the files. I am running the script locally on the actual computer the folder if located on. I can run the script against a single folder within the root folder and it works okay. I can also add each folder into the script text file and it will delete a few then stop with a permission issue. I assume this has something to do with a filename length or folder name length.
Again, this is not an actual permission issue as I can manually delete all of the files and folders myself. The script is run locally so it does not have permission issues.
Dim fso, startFolder, OlderThanDate
Dim strFilename, strPath
Set fso = CreateObject("Scripting.FileSystemObject")
strFilename = "c:\work\ee\urgent.txt"
OlderThanDate = DateAdd("d", -60, Date) ' 60 days (adjust as necessary)'
set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(strFilename) Then
set objFile = objFSO.GetFile(strFilename)
Set TextStream = objFile.OpenAsTextStream(1,0)
Do While Not TextStream.AtEndOfStream
strPath = trim(TextStream.readline)
DeleteOldFiles strPath, OlderThanDate
Set TextStream = nothing
Msgbox "Input File not found"
Msgbox "Old Backup files have been deleted...Done !!"
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
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Dim objFolder, objSubFolders, objFiles
Dim xFolder, xFile
'On Error Resume Next'
Set objFolder = fso.GetFolder(sFolderName)
Set objSubFolders = objFolder.SubFolders
Set objFiles = objFolder.Files
iTotFiles=iTotFiles + objFiles.count
'set to remove readonly
Const READONLY = 1
For Each xFile In objFiles
xFile.Attributes = xFile.Attributes And Not READONLY
For Each xFolder In objSubFolders
xFolder.Attributes = xFolder.Attributes And Not READONLY
'count directories found'
' If the folder is empty then delete the folder.'
If objFiles.Count <= 0 and objSubFolders.count <= 0 Then
fso.DeleteFolder sFolderName, True