WSH.Run CMDtypes代码的广义问题
以下是不是我的代码。 我不断跳动文本中的错误代码。 我被告知以下实施代码:
您必须设置对Windows Script Host Object Model
的引用。 sDrive
和sBasePath
用于设置起始文件夹名称。 sFileList
是将结果写入文本文件的地方。
我已经设置Windows Script Host Object Model
被引用,但仍然无法让它成功运行,而不会跳过内置的错误。 请帮忙
Public sDrive As String Public sBasePath As String Public Const sFileList As String = "U:\" Option Explicit Sub GetDirTree() Dim WSH As WshShell Dim lErrCode As Long Set WSH = New WshShell lErrCode = WSH.Run("cmd.exe /c dir """ & sDrive & sBasePath & """/B /S >" & sFileList, 0, True) If lErrCode <> 0 Then MsgBox ("Error in GetDirTree: Error Number: " & CStr(lErrCode)) Stop End If End Sub
Code Credit: Ron Rosenfeld
以下代码要求通过VBE的工具►引用命令将Windows脚本宿主对象模型添加到项目中。
你错过了一些variables的分配; 例如,声明时它们是vbnullstrings,而不是赋值。
Option Explicit Public sDrive As String Public sBasePath As String Public sFileList As String Sub GetDirTree() Dim WSH As WshShell Dim lErrCode As Long sDrive = "c:\" sBasePath = "New Folder\*" sFileList = Environ("TEMP") & Chr(92) & "My_Dir_List.txt" Debug.Print "cmd.exe /c dir """ & sDrive & sBasePath & """/B /S >" & sFileList Set WSH = New WshShell lErrCode = WSH.Run("cmd.exe /c dir """ & sDrive & sBasePath & """/B /S >" & sFileList, 0, True) If lErrCode <> 0 Then MsgBox ("Error in GetDirTree: Error Number: " & CStr(lErrCode)) Stop End If End Sub
我已经改变了sFileList
的声明,使其公开,但不是const。 你不能改变一个constantvariables。 sFileList
将被放入由TEMP环境variables指定的文件夹中。 这是很方便的,因为不应该有写入权限错误。 为了进行debugging,我发送了用于VBE立即窗口的命令。