Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

VBscript editing, my script bombs out when going to a folder it doesnt have rights to. need to be able to skip it

i have a code below.   it goes through all the folders but when it gets to the folder it doesnt have rights to, it gets killed.   i want it to be skip that folder it doesnt have right to and move to the next folders....
Set objShell = CreateObject("WScript.Shell")
strProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
strFolder = strProfile & "Cookies"
intDaysOld = 60
dteDate = DateAdd("d", intDaysOld, Now)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strFolder).Files
	If objFile.DateLastModified < dteDate Then
		On Error Resume Next
		objFSO.DeleteFile objFile.Path, True
		Err.Clear
		On Error GoTo 0
	End If
Next

Open in new window

0
cahillj
Asked:
cahillj
1 Solution
 
tigermattCommented:
Try just adding an "on error resume next" to the top of the script. When it crashes on a particular folder, it will just move onto the next one without erroring.

-tigermatt
on error resume next
Set objShell = CreateObject("WScript.Shell")
strProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
strFolder = strProfile & "Cookies"
intDaysOld = 60
dteDate = DateAdd("d", intDaysOld, Now)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strFolder).Files
        If objFile.DateLastModified < dteDate Then
                On Error Resume Next
                objFSO.DeleteFile objFile.Path, True
                Err.Clear
                On Error GoTo 0
        End If
Next

Open in new window

0
 
cahilljAuthor Commented:
thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now