Exec.StdIn.Write的问题

时间:2014-10-03 17:48:12

标签: vbscript cmd exec stdin winscp

我试图在VBScript中使用Exec.StdIn.Write在cmd的同一个cmd中调用cmd中的多个命令。但它似乎并没有正常工作。我不确定它是否只是某种时间问题,或者是什么。这是我的代码(或者至少是我目前正在测试的相关部分):

 Set objWSH = CreateObject("Wscript.Shell")
 Set objFSO = CreateObject("Scripting.FileSystemObject")

 strIP = "192.168.1.185"

 strFilePath = "C:\Users\adam\Documents\SQLite3\" & strIP & ".db"

 Set objWinSCP = objWSH.Exec("cmd.exe cd C:\Program Files (x86)\WinSCP") 
 objWinSCP.StdIn.Write("winscp.exe /console /script=" & strIP & ".txt")
 Do Until boolExist = True
  boolExist = objFSO.FileExists(strFilePath)
 Loop

WinSCP脚本只是让WinSCP从我的Nexus 7中提取文件。我已经在cmd中手动测试过了,它运行正常。但是,当我运行完整的VBScript时,文件永远不会被拉出。当调用Exec,然后使用StdIn.Write时,StdIn.Write会等待执行前一个Exec语句,对吗?如果它没有,那么我想这可能只是一个时间问题。老实说,我很难在StdIn.Write的细节上找到很多东西。我甚至看到人们建议使用StdIn.Writeline。虽然我不确定那里有什么区别。说实话,我之前只使用过一次或两次Exec方法。这仅适用于cmd中的一个命令调用。所以,我有点迷失在这里。

我也尝试过只使用一次Exec调用来完成这项工作。但这也不起作用。无论如何,我需要让它全部正常工作,因为我的脚本的另一部分需要cmd实例中的多个命令。

我还检查过以确保我有最新版本的Windows Script Host for Exec。我有Ver。 5.8,这是最新版本。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试下一步方法:

' VB Script Document issues-with-exec-stdin-write_done
option explicit
On Error GoTo 0
Dim sResult: sResult = Wscript.ScriptName
Dim objWSH, oExec, cmdLine

Set objWSH = CreateObject( "Wscript.Shell")

cmdLine = "cmd.exe /K CD ""C:\Program Files\""&dir A*&pause"
' minimum: 
' cmdLine = "cmd.exe /K"
'
Set oExec = objWSH.Exec( cmdLine)
' follows in next line: wait for StdOut stream (MyStd_IO function)
sResult = sResult & vbNewLine & MyStd_IO( "", "vesy...", 30)

cmdLine = "CD %userprofile%&dir a*&pause" & VbCrLf
' CD %userprofile% : a cmd command to run (an example)  
' &                : ampersand indicates multiple commands line 
' dir a*           : other, next cmd command to run (an example)
' &                : 
' pause            : force (unique) determined string in StdOut stream
'                  : 
' VbCrLf           : tell "our command line ends" to command processor
'
' follows in next line: run some commands, collect StdOut stream 
sResult = sResult & vbNewLine & MyStd_IO( cmdLine, "vesy...", 30)
' cmdLine    : our (multiple commands) command line
' "vesy..."  : PAUSE command standard output (ending in my language)
' 30         : time limit (in seconds) to wait for std. output

sResult = sResult & vbNewLine & MyStd_IO( "exit", "", 1)

Set oExec = Nothing
Wscript.Echo sResult
Wscript.Quit

Function MyStd_IO( byval strCmd, byval strTxt, byval secsLimit)
' Provides access to the StdIn and StdOut (not StdErr) streams
'   of a WshScriptExec Object (within a script run with Exec Method) 
' Returns StdOut output stream of the oExec object
' Input parameters:
' strCmd    : text to write to StdIn input stream of the oExec object
'           : (no StdIn.Write if empty string)
' strTxt    : text stating end of StdOut output stream of the oExec object
'           : (no StdOut.Read if empty string). Language dependent,
'           : defaults to English output of PAUSE command
'           : "any key to continue..."
' secsLimit : time limit (in seconds) to wait for StdOut output stream
'           : (waits infinite if zero)
'
  MyStd_IO = "***" & vbTab & strCmd & vbNewLine 
  Dim ii : ii = 0 '' loop pass counter
  If oExec.Status = 0 Then 
    Dim timeStart :  timeStart = Now
    MyStd_IO = MyStd_IO & vbNewLine
    If strCmd = "" Then
    Else
      oExec.StdIn.Write( strCmd)
    End If
    If strTxt = "" Then
      ' no 
    Else
      Do While True
        ii = ii + 1
        If Not oExec.StdOut.AtEndOfStream Then
            MyStd_IO = MyStd_IO & oExec.StdOut.Read(1)
            If InStr( MyStd_IO, strTxt) <> 0 _
              Or InStr( MyStd_IO, "any key to continue...") <> 0 _
                Then Exit Do
        End If
        WScript.Sleep 1
        If secsLimit = 0 Or DateDiff( "s", timeStart, Now) < secsLimit Then
        Else
          MyStd_IO = MyStd_IO & vbNewLine & "!!! MyStd_IO" & "!!! time limit !!!"
          Exit Do
        End If
      Loop
      If oExec.Status = 0 Then oExec.StdIn.Write( VbTab) 'any key to continue
    End If
  Else
    MyStd_IO = MyStd_IO & " WshScriptExec object status: WshFinished"
  End If
  MyStd_IO = vbNewLine & ii & "× " & MyStd_IO 'add  
End Function