sam
sam

Reputation: 9

VBscripting _error

My requirement is to search entire c:\ drive and export the entries to a .txt file format. The challange is user needs to input the filename in a dialog box.

Following is the script that I created, when I run this I am getting an error permission denied.

'On Error Resume Next
Option Explicit 'force all variables to be declared

Const ForWriting = 2
Dim objFSO
Dim wshell
Set wshell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

wshell.ExpandEnvironmentStrings("%SYSTEMDRIVE%")

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\Eurofins\Logs\Output.txt", ForWriting, True)

Recurse objFSO.GetFolder("%SYSTEMDRIVE%")
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "ini" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "txt" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

Upvotes: 0

Views: 42

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200483

There are some folders on the system drive where you don't have access. Not even as an administrator. This behavior is by design.

Probably the simplest way to deal with this would be to enable error handling inside your procedure:

Sub Recurse(objFolder)
    On Error Resume Next
    ...
End Sub

Note, however, that this would suppress all errors, leaving you completely in the dark if anything goes wrong. A better way would be to only ignore specific error numbers:

...
For Each objFile In objFolder.Files
    ...
Next
If Err.Number <> 0 Then
    If Err.Number <> 70 Then
        WScript.Echo Hex(Err.Number) & ": " & Err.Description
        'further error handling goes here
    End If
End If
...

Upvotes: 1

Related Questions