bob_kochanski
asked on
I am trying to delete all files in folders and subfolders older than 7 days from a share on the network
We have just implemented a SAN solution and I am trying to run a script (that works on the local server) that looks to a share across the network to delete all files in folders and subfolders and I am receiving an error message stating:
Line: 16
Char: 11
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
I am logged in as a domain admin and have full rights to the server that I am trying to kick it off on.
I have attached the script, however, if I run it on the local server and point to the drive letter it works fine.
Any help would be greatly appreciated.
Line: 16
Char: 11
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
I am logged in as a domain admin and have full rights to the server that I am trying to kick it off on.
I have attached the script, however, if I run it on the local server and point to the drive letter it works fine.
Any help would be greatly appreciated.
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "\\sadvnfs01\Scan-DVN\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -7, Date) ' 7 days (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
End Function
ASKER
Alright so I changed the script to match the days old and the path. When I execute the script I don't get any errors, however, it's not doing anything. I saved it as a .vbs script, right? I have attached the changes that I made.
Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
'Customize values here to fit your needs
iDaysOld = 7
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "\\sadvnfs01\Scan-DVN"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'Walk through each file in this folder collection.
'If it is older than '7' days, then delete it.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yeah, it sounds like a permissions issue then. Can you go to explorer and explore to \\sadvnfs01\Scan-DVN and try and delete the files manually? Does that work correctly? If not, then it is a permissions issue somewhere. If you can, how are you running these vbs scripts? By double clicking on them? or by scheduled job? If by scheduled job then you'll need to put in a 'run as' account information into the scheduled job.
ASKER
That worked perfectly.
Thanks again,
Thanks again,
No problem. Thanks for the grade. Did you have to modify any permissions entries?
Regards,
Rob.
Regards,
Rob.
Open in new window