Solved

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

Posted on 2009-05-15
6
1,870 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 21

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 21

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 21

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 21

Author Closing Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

13 Experts available now in Live!

Get 1:1 Help Now