2014-10-03 5 views
1

VBScript에서 Exec.StdIn.Write를 사용하여 cmd의 동일한 인스턴스에서 cmd의 여러 명령을 호출하려고합니다. 하지만 제대로 작동하지 않는 것 같습니다. 타이밍 문제 일 뿐인 지 어떤지는 확실하지 않습니다.Exec.StdIn.Write와 관련된 문제

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에 수동으로 cmd를, 그것은 및 작동 내가 테스트 한 내 넥서스 7에서 파일을 끌어했습니다 여기 내 코드 (I 현재 테스트입니다 또는 적어도 관련 부분)의 벌금. 그러나 전체 VBScript를 실행하면 파일을 절대 가져 오지 않습니다. Exec가 호출되고 차례로 StdIn.Write가 사용되면 StdIn.Write는 이전 Exec 문의 실행을 기다립니다. 맞습니까? 그렇지 않다면 아마도 타이밍 문제 일 것입니다. 솔직히 StdIn.Write의 세부 사항에 대해 많은 것을 찾는 데 문제가 있습니다. 심지어 사람들이 StdIn.Writeline을 사용하여 제안하는 것을 보았습니다. 나는 그 차이점이 무엇인지 모르겠다. 솔직히 말해서, 실제로 한두 번 Exec 메서드를 실제로 사용한 적이 있습니다. 그리고 이것은 cmd에서 한 명령 호출에 대한 것입니다. 그래서 나는 여기서 잃어 버렸습니다.

이 작업을 수행하기 위해 하나의 EXEC 호출 만 사용하려고했습니다. 그러나 그것도 작동하지 않습니다. 그리고 cmd 인스턴스에서 여러 명령을 필요로하는 스크립트의 또 다른 부분이 있기 때문에 모든 것을 작동시켜야합니다.

또한 최신 버전의 Windows Script Host가 포함되어 있는지 확인했습니다. 그리고 나는 Ver. 5.8, 최신 버전입니다.

도움이 될 것입니다.

+0

당신은 단지'.Exec' 하나의 라이너를 만들 수 있을까요? cmd.exe C : \ Program Files (x86) \ WinSCP \ winscp.exe/console/script = "& strIP &".txt ". 나는 파일 경로 주위에 몇 가지 따옴표를 넣을 것이다. – Matt

+0

나는 할 수 있었다. 사실입니다. 나는 주로 그것을 나누고 싶었다. 하지만 StdIn.Write를 가져와야 할 필요가 있습니다. 이후 스크립트의 다른 부분에 사용해야합니다. 다른 부분은 6 배 정도 길기 때문에 방금이 부분을 게시했습니다. 나는 그것을 미리 언급해야한다고 생각합니다. 그러나 한 줄만 사용하면이 문제가 처음부터 Exec 호출에있는 것으로 드러났습니다. 하나의 EXEC 호출도 작동하지 않습니다. 하지만이 모든 것을 설명하기 위해 원래 게시물을 편집 할 것입니다. –

답변

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 
+0

도움에 감사드립니다. 그러나 나는 그것을 시험해 보았다. 그리고 그것은 아직도 효과가 없다. 그냥 무한정 걸립니다. 나는 대기 시간을 40 초로 제한하려고 시도하기까지했다. 아직 아무것도. 그래서 그게 이그제큐티브에서 문제가 될지도 모른다고 생각합니다. 가능한 한 간단한 명령으로 Exec을 테스트 해보고 무슨 일이 일어나는지 살펴 보겠습니다. 제대로 실행되지 않으면 Exec과 관련하여 문제가 있음을 알게 될 것입니다. 테스트 할 기회가 생기면 그 결과를 원래 게시물에 추가 할 것입니다. –