奇怪的VBScript错误:需要对象:' objFolder'

时间:2016-06-27 14:27:15

标签: vbscript

我在互联网上进行了广泛的搜索,但仍无法找到解决方案。有趣的是我的代码以前工作过。我正在使用带有VBScript代码的html页面,使用IE 9打开。

我的代码如下:

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

我在第30行显示错误:需要对象&#39; objFolder&#39;

我在第31行添加了一个消息框,它已到达,在给定文件夹中输出带有许多子文件夹的消息框。如果问题实际上在第30行,它将永远不会到达第31行。如果我完全删除第31行(带有消息框的那个),我仍然在第30行得到相同的错误。

我的上述功能称为以下方式:

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

该文件夹存在并且检索没有问题。不确定发生了什么。有人可以对这个问题有所了解吗?

1 个答案:

答案 0 :(得分:3)

下一个脚本按照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

调试方案

==> 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

输出显示文件夹树中的叶子被处理两次,因此上面的脚本需要更多思考和调试:请注意strResult变量已更新,而不是原始 ProcessFolder致电:

==> 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