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
Solved

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

Posted on 2009-05-15
6
1,940 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
  • 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 22

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 22

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 22

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 22

Author Closing Comment

by:yo_bee
ID: 31581914
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.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

840 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