2014-03-25 124 views
0

有誰知道爲什麼會發生下面的情況,有沒有人有解決方法?捕獲(失敗)mklink命令輸出

我strugging捕捉mklink命令輸出(通過cmd.exe的mklink> out.txt)

的輸出被髮送到out.txt細如果mklink命令成功

EG:%comspec% /c mklink /d C:\Test C:\Windows > out.txt && notepad out.txt

但是,如果命令是無效的,或失敗,那麼什麼都不會被寫入到out.txt

EG:Run above command again(失敗,因爲C:\測試已經存在)或

E.G:%comspec% /c mklink > out.txt && notepad out.txt

我使用VBScript中的命令,沒有任何人知道如何捕捉mklink輸出如果命令沒有成功完成?

Set o_shell = CreateObject("Wscript.Shell") 
Set o_fso = CreateObject("Scripting.FileSystemObject") 
mklinkCommandOutput = GetCommandOutput("mklink /D ""C:\Test"" ""C:\Windows""") 
WScript.echo mklinkCommandOutput 

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 

回答

1

(1)根據Using multiple commands and conditional processing symbols,符號&&只運行,如果左側命令成功右邊的命令。即使mlink失敗,您也必須使用& 開始記事本。

(2)雖然mlink docs不說那麼明確,我認爲mlink寫入其錯誤消息到stderr(見here) - 就像dir

證據:(RSP dir

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 

將Stderout Stderr重定向到輸出文件。

證據:

Dos & Notepads

+0

只是想出來 - 歡呼聲!我只是使用&&記事本來嘗試打開輸出文件以向大家展示,但再次感謝 – aciid

+0

使用命令鏈接提示。我不太瞭解你在答案中有時使用的證據區塊。我得到它是代碼執行的實時日誌。我只是沒有看到這麼多的解釋。 – Rich

1

您是否考慮過使用「Exec」命令而不是運行命令並收集輸出結果?

它不需要文件,它更容易。

新代碼

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

舊代碼

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

我以前使用Exec的,甚至加入了循環等待命令狀態試過了,但它總是相同的結果,因爲如果我運行此: HTTP:/ /pastebin.com/raw.php?i=axHVLLSz – aciid

+1

好吧'GetCommandOutput = oExec.StdErr.ReadAll'工作:)問題解決了,甜心 – aciid

+0

很高興它爲你工作兄弟。請享用。 – Rich