2014-03-25 6 views
0

아래의 이유를 알고있는 사람이 있습니까? 누구에게도 해결책이 있습니까? 내가 mklink 명령 출력을 캡처 strugging있어(실패한) mklink 명령 출력 출력

(를 통해 cmd.exe를 mklink> out.txt)

출력은 mklink 명령

EG 성공하면 좋은 out.txt에 전송됩니다 %comspec% /c mklink /d C:\Test C:\Windows > out.txt && notepad out.txt

명령이 유효하지 않거나 실패 할 경우

그러나, 다음 아무것도

EG out.txt에 기록되지 않습니다 : 또는

: Run above command again을 (\ 테스트가 이미 존재 C가 있기 때문에 실패)

예컨대 : 나는 VBScript를에서 명령을 사용하고 %comspec% /c mklink > out.txt && notepad out.txt

, 사람이 명령이 성공적으로 완료되지 않을 경우 mklink 출력을 캡처하는 방법을 알고 있는가?

답변

1

. mlink이 실패한 경우에도 메모장을 시작하려면 & 을 사용해야합니다.

(2) mlink docs 그렇게 명시 적으로 언급하지 않지만, 나는 mlink이 (here 참조) stderr에 그 오류 메시지를 기록한다고 가정 - 단지 dir처럼.

증거 : (. dir을 RSP)

dir 01.vbs 
... 
19.10.2012 11:29    2.588 01.vbs 
... 
(dir succeeded) 

dir nix 
... 
File Not Found 
(dir failed) 

dir nix && echo nothing to see, because lefty failed 
... 
File Not Found 
(dir failed, no output because of &&) 

dir nix & echo much to see, although lefty failed 
... 
File Not Found 
much to see, although lefty failed 
(dir succeeded, echo done because of &) 

(3) 실패 여부를 mlink의 출력을 캡처 여부 및 메모장에 결과 (파일)을, 당신이 사용하는 한 표시하려면

dir 01.vbs 1> out.txt 2>&1 & notepad out.txt 
dir nix 1> out.txt 2>&1 & notepad out.txt 

Stdout Stderr을 출력 파일로 리디렉션 할 수 있습니다.

증거 : 심지어 명령 상태를 기다리는 루프를 추가하기 전에 Exec을 사용하여 시도했지만, 내가 이것을 실행하는 것처럼이 항상 같은 결과입니다

Dos & Notepads

+0

그걸 알아 냈어. 건배! 나는 && 메모장을 사용하여 출력 파일을 열어서 너희에게 보여 주려고했으나 팁을 다시 주셔서 감사한다. – aciid

+0

+1 명령 링크를 사용했다. 가끔 대답에 사용하는 증거 블록을 이해하지 못합니다. 나는 코드 실행의 실시간 로그를 얻는다. 나는 그 정도의 설명을 보지 못했다. – Rich

1

실행 명령 대신 "실행"명령을 사용하고 출력 결과를 수집하는 것이 고려 되었습니까?

파일을 필요로하지 않으며 단지 더 쉽습니다.

새로운 코드

Function GetCommandOutput(runCmd) 
    Dim WshShell, oExec 
    Set WshShell = CreateObject("WScript.Shell") 
    Set oExec = WshShell.Exec("%COMSPEC% /c " & runCmd) 
    GetCommandOutput = oExec.StdOut.ReadAll 
End Function 

기존 코드 (1) Using multiple commands and conditional processing symbols에 따르면, 기호 && 왼쪽에있는 명령이 성공하는 경우에만 오른쪽에있는 명령을 실행

Function GetCommandOutput(runCmd) 
    on error resume next 
    Dim o_file, tempFile: tempFile = o_shell.ExpandEnvironmentStrings("%TEMP%") & "\tmpcmd.txt" 

    ' Run command and write output to temp file 
    o_shell.Run "%COMSPEC% /c " & runCmd & " > """ & tempFile & """", 0, 1 

    ' Read command output from temp file 
    Set o_file = o_fso.OpenTextFile(tempFile, 1) 
    GetCommandOutput = o_file.ReadAll 
    o_file.Close 

    ' Delete temp file 
    Set o_file = o_fso.GetFile(tempFile) 
    o_file.Delete 
End Function 
+0

: HTTP ://pastebin.com/raw.php?i=axHVLLSz – aciid

+1

좋아,'GetCommandOutput = oExec.StdErr.ReadAll' 작품 :) 문제 해결, 달콤한 – aciid

+0

다행 당신을 위해 일해. 즐겨. – Rich