有誰知道爲什麼會發生下面的情況,有沒有人有解決方法?捕獲(失敗)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
只是想出來 - 歡呼聲!我只是使用&&記事本來嘗試打開輸出文件以向大家展示,但再次感謝 – aciid
使用命令鏈接提示。我不太瞭解你在答案中有時使用的證據區塊。我得到它是代碼執行的實時日誌。我只是沒有看到這麼多的解釋。 – Rich