1

I have done extensive search on internet but still was not able to find the solution. The interesting thing is that my code worked before. I am using html page with VBScript code, opened using IE 9.

My code is below:

29: Function TraverseDirectory(objFolder, searchTerm, outFile)
30: if objFolder.SubFolders.Count > 0 then <-- ERROR shown in this line: Object required: 'objFolder' 
31:     MsgBox objFolder.SubFolders.Count <-- This message is shown without an issue
32:     Set fc = objFolder.SubFolders
33:         For Each f1 in fc
34:         ProcessFolder f1, searchTerm, outFile
35:             TraverseDirectory f1, searchTerm, outFile
36:         Next
37: else
38:     ProcessFolder objFolder, searchTerm, outFile
39: end if
40: End Function

I am showing the error in line 30: Object required 'objFolder'

I added a message box in line 31 and it was reached, outputting message box with a number of subfolders in a give folder. If the problem was actually in line 30, it would never reach line 31. If I completely remove line 31 (the one with a message box), I still get the same error in line 30.

My function above is called the following way:

Set objFolder = objFSO.GetFolder("C:\Test")
TraverseDirectory objFolder, str, outFile

The folder exists and is retrieved without a problem. Not sure what is happening. Can someone shed some light on the issue?

5
  • I can't reproduce the issue, but it might be a scoping issue with the objFolder variable. Can you test this by changing objFolder to something unique in the function, like objFolderInsideFunction? Commented Jun 27, 2016 at 20:13
  • 1
    What output from MSGBOX VarType( objFolder) & vbTab & TypeName(objFolder) between lines 29 and 30 and before the first TraverseDirectory? Commented Jun 27, 2016 at 20:44
  • the output is "8 Folder" followed by "9 Nothing" Commented Jun 27, 2016 at 20:53
  • I figured it out! your comment helped me diagnose it. Can you post it as an answer so i could accept it? Commented Jun 27, 2016 at 21:01
  • Actually there is one more issue when I am looping through subfolders - Set fc = objFolder.SubFolders For each f1 in fc the output come out as "8 String" instead of "8 Folder" Commented Jun 27, 2016 at 21:09

1 Answer 1

3

Next script collects/echoes some debugging info as advised in my previous comment

option explicit
'On Error Resume Next
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim objfso, str, outfile,  objFolder
set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("D:\TestC")
'Set objFolder = objFSO.GetFolder("C:\attachments") 'an empty folder for debugging'
Wscript.Echo "start" & vbTab _
    & VarType( objFolder) & " " & TypeName(objFolder) & vbTab & objFolder
TraverseDirectory objFolder, str, outFile
Wscript.Echo strResult
Wscript.Quit

Function TraverseDirectory(objFolder, searchTerm, outFile)
  Dim fc, f1, aux
  Wscript.Echo "debug" & vbTab _
      & VarType( objFolder) & " " & TypeName(objFolder) & vbTab & objFolder
  aux = objFolder.SubFolders.Count
  if aux > 0 then  '<-- ERROR shown in this line: Object required: 'objFolder' 
      'MsgBox objFolder.SubFolders.Count  ' <-- This message is shown without an issue
      Set fc = objFolder.SubFolders
          For Each f1 in fc
              strResult = strResult & vbNewLine & Cstr( aux) _
                  & vbTab & VarType( f1) & " " & TypeName(f1) & vbTab & f1
              'ProcessFolder f1, searchTerm, outFile
              TraverseDirectory f1, searchTerm, outFile
          Next
  else
      'ProcessFolder objFolder, searchTerm, outFile
      strResult = strResult & vbNewLine & Cstr( aux) & vbTab _
          & VarType( objFolder) & " " & TypeName(objFolder) & vbTab & objFolder
  end if
End Function

Debugging scenario:

==> tree "D:\TestC"
Folder PATH listing for volume DataDisk
Volume serial number is … … …
D:\TESTC
├───bubu
│   └───foobar
├───kuku
├───New Folder 12
└───New Folder 21
    └───New folder XX

Output shows that leafs in folder tree are processed twice so the script above requires more thinking and debugging: note that strResult variable is updated in place of original ProcessFolder call:

==> cscript D:\VB_scripts\SO\38056552.vbs
start   8 Folder        D:\testC
debug   8 Folder        D:\testC
debug   8 Folder        D:\testC\bubu
debug   8 Folder        D:\testC\bubu\foobar
debug   8 Folder        D:\testC\kuku
debug   8 Folder        D:\testC\New Folder 12
debug   8 Folder        D:\testC\New Folder 21
debug   8 Folder        D:\testC\New Folder 21\New folder XX
38056552.vbs
4       8 Folder        D:\testC\bubu
1       8 Folder        D:\testC\bubu\foobar
0       8 Folder        D:\testC\bubu\foobar
4       8 Folder        D:\testC\kuku
0       8 Folder        D:\testC\kuku
4       8 Folder        D:\testC\New Folder 12
0       8 Folder        D:\testC\New Folder 12
4       8 Folder        D:\testC\New Folder 21
1       8 Folder        D:\testC\New Folder 21\New folder XX
0       8 Folder        D:\testC\New Folder 21\New folder XX
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.