Solved

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

Posted on 2008-10-10
2
204 Views
Last Modified: 2010-04-21
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
Comment
Question by:cahillj
2 Comments
 
LVL 58

Accepted Solution

by:
tigermatt earned 500 total points
ID: 22688525
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
 

Author Closing Comment

by:cahillj
ID: 31505084
thank you!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question