0

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
4
  • Welcome to stackoverflow. On what line do you get the error? Commented Mar 29, 2017 at 10:01
  • line 16 ,1 Recurse objFSO.GetFolder("%SYSTEMDRIVE%") Commented Mar 29, 2017 at 10:58
  • You call wshell.ExpandEnvironmentStrings("%SYSTEMDRIVE%") but you don't assign it to a variable, you then call Recurse objFSO.GetFolder("%SYSTEMDRIVE%") without the expanded version which you should have got by assigning the variable in the first place. It will error because FSO doesn't know what %SYSTEMDRIVE% is, that's why you use wshell.ExpandEnvironmentStrings("%SYSTEMDRIVE%"). Commented Mar 29, 2017 at 16:56
  • Didn't you say you were "getting an error permission denied."? Commented Mar 29, 2017 at 16:57

1 Answer 1

1

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
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.