Function Run_Cmd(strCmd)
Dim objShell
Dim objScriptExec
Dim strCmdResult
strCmd = "%comspec% /C " + strCmd
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.exec(strCmd)
set Run_Cmd = objScriptExec.StdOut
End Function
Function test ()
'...
Set objPropFilesList = Run_Cmd("dir /B " & sStarterDir & " | findstr /I """&test_list&"""")
if ( objPropFilesList.count = 0 ) Then
LogWrite "No EPM services found to verify... Aborting execution.", fAutoFixLog, bLogToConsole
wscript.echo "No EPM services found to verify... Aborting execution."
Exit Function
End If
Do Until objPropFilesList.AtEndOfStream
'...
Loop
End Function
在上面的代码中,objPropFilesList返回文本流。当我放置if条件来检查计数时,它会跳过此函数中的剩余代码。我不明白为什么它会跳过这段代码。
我的怀疑是,按照以下doc Count Property处理Dictionary对象。 objPropFilesList返回文件名列表,这不会被视为字典对象。
https://msdn.microsoft.com/en-us/library/ea5ht6ax(v=vs.84).aspx
我想了解这里到底发生了什么。
答案 0 :(得分:-1)
您正在运行控制台命令并在文本流中捕获输出。这只是文字。您可以转换为一系列行。 A = Split(A, vbcrlf)
。
这显示了如何在VBScript中执行文件操作。这使用递归来提供dir /s
类型的操作。
'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
ProcessFolder DirName
Sub ProcessFolder(FolderPath)
On Error Resume Next
Set fldr = fso.GetFolder(FolderPath)
Set Fls = fldr.files
For Each thing in Fls
msgbox Thing.Name & " " & Thing.path
Next
Set fldrs = fldr.subfolders
For Each thing in fldrs
msgbox Thing.Name & " " & Thing.path
ProcessFolder thing.path
Next
End Sub
答案 1 :(得分:-1)
演示:
WTF
Option Explicit
Function Run_Cmd(ByVal strCmd) ' nasty surprises without ByVal as you change strCmd in the function
' Dim strCmdResult - not used; proves that 'long' Dim lists 'far' from the use of the variables are a bad idea
strCmd = "%comspec% /C " + strCmd
Set Run_Cmd = CreateObject("WScript.Shell").exec(strCmd).StdOut
End Function
Dim sPat : sPat = "vbs"
If 1 <= WScript.Arguments.Count Then sPat = WScript.Arguments(0)
Dim tsCmd : Set tsCmd = Run_Cmd("dir /B .\* | findstr /I """ & sPat & """")
WScript.Echo TypeName(tsCmd)
' OERN *hides* errors
On Error Resume Next
tsCmd = Split(tsCmd, vbCrLf)
WScript.Echo Err.Number, Err.Description
On Error GoTo 0
' use tsCmd.AtEndOfStream to determine whether to log before you loop over the lines
If tsCmd.AtEndOfStream Then
WScript.Echo "Log: No EPM"
Else
Do Until tsCmd.AtEndOfStream
WScript.Echo tsCmd.ReadLine()
Loop
End If
输出:
cscript 41887615.vbs
TextStream
438 Das Objekt unterstützt diese Eigenschaft oder Methode nicht.
41887615.vbs
cscript 41887615.vbs pipapo
TextStream
438 Das Objekt unterstützt diese Eigenschaft oder Methode nicht.
Log: No EPM