Solved

VB Script Help

Posted on 2013-01-17
4
270 Views
Last Modified: 2013-01-17
I am running the following script to delete files from a folder upon login.  The script works fine, but the problem is not all users have this folder path.  Is there a way to supress the path not found error?  Applying script to certain GPOs is not an option.

Set objShell = CreateObject( "WScript.Shell" )
strLocalAppData=objShell.ExpandEnvironmentStrings("%LocalAppData%")
strFolder = strLocalAppData & "\Cisco\Unified Communications\Jabber\CSF\Photos"
Set fso = CreateObject("Scripting.FileSystemObject")
For Each objFile In fso.GetFolder(strFolder).Files
'  Wscript.Echo "Deleting file: " & objFile.Path
  objFile.Delete
Next
For Each objSubfolder In fso.GetFolder(strFolder).Subfolders
'  Wscript.Echo "Deleting folder: " & objSubfolder.Path
  objSubfolder.Delete
Next

Open in new window

0
Comment
Question by:Kram80
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
dustock earned 250 total points
ID: 38788383
Add On Error Resume Next before you call the delete, if it encounters an error it will continue through the loop.

You can google 'vbscript error handling' for more information
0
 

Author Comment

by:Kram80
ID: 38789083
Thanks for the help.  Appreciate it.
0
 
LVL 9

Expert Comment

by:dustock
ID: 38789193
No problem
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 38790030
As an alternate you can do this, and not have to disable error checking.

Set objShell = CreateObject( "WScript.Shell" )
Set fso = CreateObject("Scripting.FileSystemObject")
strFolder = objShell.ExpandEnvironmentStrings("%LocalAppData%\Cisco\Unified Communications\Jabber\CSF\Photos")
If fso.FolderExists(strFolder) Then
  For Each objFile In fso.GetFolder(strFolder).Files
  '  Wscript.Echo "Deleting file: " & objFile.Path
    objFile.Delete
  Next
  For Each objSubfolder In fso.GetFolder(strFolder).Subfolders
  '  Wscript.Echo "Deleting folder: " & objSubfolder.Path
    objSubfolder.Delete
  Next
End If

Open in new window

~bp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now