• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2262
  • Last Modified:

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

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
yo_bee
Asked:
yo_bee
  • 4
1 Solution
 
Patrick MatthewsCommented:
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
 
yo_beeDirector of ITAuthor Commented:
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
 
DK_UserCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
yo_beeDirector of ITAuthor Commented:
I was unable to figure out a way to catch the Access Denied folder and move on.

Any more ideas
0
 
yo_beeDirector of ITAuthor Commented:
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
 
yo_beeDirector of ITAuthor Commented:
I was pointed, but never really got this to work.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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