Solved

VBS FileSystemOdject Access Denied Error.  How to get around it?

Posted on 2009-05-15
6
2,010 Views
Last Modified: 2012-06-21
I have a basic Resursive Search  using FileSystemObject VBS to find a certain shortcut.  It worked fine on my home computer, but when I try to run it on my work computer I get Permission Denied Run Time error.
I modified the the code to list all Folders and Files for testing purposes,  but the goal is to use
If instr(objSubFolder.Files,"Shortcutname") then
do somthing
else
move on
If I get Permission denied I would like to just move on to the next directory or file.

C:\Documents and Settings\All Users\Application Data\Symantec\SRTSP\SrtTxFEvt.log
C:\Documents and Settings\All Users\Application Data\Symantec\SRTSP\SrtViEvt.log
C:\Documents and Settings\boscam\My Documents\scripts\dirsearcher.vbs(16, 5) Microsoft VBScript runt
ime error: Permission denied
Const FOR_READING = 1
strFolder = "C:\Documents and Settings\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
WScript.Echo objFolder.Path
set colFiles = objFolder.Files
For Each objFile In colFiles
 wscript.echo objfile.path	
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
  Set colFolders = objFolder.SubFolders
  For Each objSubFolder In colFolders
  WScript.Echo objSubFolder.Path
    Set colFiles = objSubFolder.Files
    For Each objFile In colFiles
wscript.echo objfile.path	
   Next
   ShowSubFolders(objSubFolder)
 Next
 
End Sub

Open in new window

0
Comment
Question by:yo_bee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 24396358
The only error handling available in VBScript is On Error Resume Next.  Demo:


On Error Resume Next

'do something

If Err <> 0 Then
    Err.Clear
    'code for what to do when there is an error
End If



So you will need to integrate that into your code.  Kep in mind that that error handling has a limited scope, so
if you put it into the main body of the script, it will not apply when you branch into subs and/or functions.
0
 
LVL 23

Author Comment

by:yo_bee
ID: 24396634
I also tried this code, but I receive a different error

c:\documents and settings\administrator\application data\identities\{2110114e-a5de-4f63-97ac-6795f6f
b7b67}
C:\Documents and Settings\boscam\My Documents\scripts\dirsearcher1.vbs(19, 5) (null): 0x80041017
I figured that which ever code gets me to the final goal I will use.

I was unable to understand where to apply the
On Error Resume Next

'do something

If Err <> 0 Then
    Err.Clear
    'code for what to do when there is an error
End If


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
strFolderName = "c:\Documents and Settings"
 
Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")
 
For Each objFolder in colSubfolders
    GetSubFolders strFolderName
Next
 
Sub GetSubFolders(strFolderName)
    Set colSubfolders2 = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
            & "Where AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")
 
    For Each objFolder2 in colSubfolders2
        strFolderName = objFolder2.Name
        Wscript.Echo objFolder2.Name
        GetSubFolders strFolderName
    Next
End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:DK_User
ID: 24400783
As matthew suggested, the Error handling should get u there.
Putting "On Error Resume Next" on top in your file forces vbs to continue
In the exampel i force an error by using "C:\System Volume Information" and then you can do some stuff depending on what error you get.
Heres a little link to some info
http://www.computerperformance.co.uk/Logon/VBScript/VBScript_Windows_error_handling.htm
Or try google - vbscript On error


On Error Resume Next
 
Const FOR_READING = 1
strFolder = "C:\System Volume Information"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
 
set colFiles = objFolder.Files
 
 
For Each objFile In colFiles
	If err.number <> 0 then
		If err.number = 70 Then
			wscript.echo err.number & " " & err.description
		Else
			wscript.echo "Another error: " & err.number & " " & err.description 
		end if
		'As you have done something to the error, clear it
		err.Clear
	end if	
Next
 
 
ShowSubFolders(objFolder)
 
Sub ShowSubFolders(objFolder)
 
wscript.echo "Still going"
  Set colFolders = objFolder.SubFolders
  For Each objSubFolder In colFolders
	If err.number <> 0 then
		If err.number = 70 Then
			wscript.echo err.number & " " & err.description
		Else
			wscript.echo "Another error: " & err.number & " " & err.description 
		end if
		'As you have done something to the error, clear it
		err.Clear
	end if	
 
    	Set colFiles = objSubFolder.Files
 
	For Each objFile In colFiles
		If err.number <> 0 then
		If err.number = 70 Then
			wscript.echo err.number & " " & err.description
		Else
			wscript.echo "Another error: " & err.number & " " & err.description 
		end if
		'As you have done something to the error, clear it
		err.Clear
	end if		
	Next
   	ShowSubFolders(objSubFolder)
 Next
	
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Author Comment

by:yo_bee
ID: 24503548
I was unable to figure out a way to catch the Access Denied folder and move on.

Any more ideas
0
 
LVL 23

Author Comment

by:yo_bee
ID: 24503653
Here is my revamped Recursive Folder/File Search

I will search all subdirectories, but when a Folder name is to long it will not process and move on to the next subfolder.

What would cause this?

If I apply ON ERROR RESUME NEXT just stops the script without any error display.

Imports System
Imports System.IO
 
 
Public Class Form1
 
    Private Sub FolderBrowserDialog1_HelpRequest(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FolderBrowserDialog1.HelpRequest
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
 
        End If
 
    End Sub
 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim StrFiles As String
 
        StrFiles = TextBox1.Text
 
        SaveFileDialog1.Title = "Please Select a File"
        SaveFileDialog1.InitialDirectory = "C:\"
        SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        SaveFileDialog1.ShowDialog()
 
        Dim f, d
 
        
        Using sw As StreamWriter = New StreamWriter(SaveFileDialog1.FileName)
            sw.WriteLine("Directory Path" & vbTab & "File Name" & vbTab & "Date Modified")
            f = 0
            d = 0
            sw.WriteLine(StrFiles)
 
 
 
            For Each folder In Directory.GetDirectories(StrFiles)
                Dim SubDI As New DirectoryInfo(folder)
                Dim SubFI As FileInfo() = SubDI.GetFiles()
                Dim SubFItemp As FileInfo
                sw.WriteLine(folder)
                d = d + 1
                For Each SubFItemp In SubFI
                    sw.WriteLine(vbTab & SubFItemp.Name & vbTab & SubFItemp.LastWriteTime)
                    f = f + 1
 
                Next
 
                For Each subdir In Directory.GetDirectories(folder)
                    Dim SubDi2 As New DirectoryInfo(subdir)
                    Dim SubFI2 As FileInfo() = SubDi2.GetFiles()
                    Dim SubFiTemp2 As FileInfo
                    sw.WriteLine(subdir)
                    d = d + 1
                    For Each SubFiTemp2 In SubFI2
                        sw.WriteLine(vbTab & SubFiTemp2.Name & vbTab & SubFiTemp2.LastWriteTime)
                        f = f + 1
                    Next
 
                Next
 
 
            Next
 
 
 
 
 
 
        End Using
        RichTextBox1.Text = ("Search has completed and outputed to: " & SaveFileDialog1.FileName & vbCrLf _
                             & "Number of Directories searched: " & d & vbCrLf _
                             & "Number of Files searched: " & f)
 
    End Sub
 
    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
 
    End Sub
 
    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
 
 
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
 
 
 
 
    End Sub
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub
End Class

Open in new window

0
 
LVL 23

Author Closing Comment

by:yo_bee
ID: 31581914
I was pointed, but never really got this to work.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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