有谁知道为什么会发生以下情况并且有人有解决方法吗?
我正在努力捕获 mklink 命令输出(通过 cmd.exe mklink > out.txt)
如果 mklink 命令成功,输出将发送到 out.txt
例如:%comspec% /c mklink /d C:\Test C:\Windows > out.txt && notepad out.txt
但是,如果命令无效或失败,则不会将任何内容写入 out.txt
EG:(Run above command again
失败,因为 C:\Test 已经存在)或
例如:%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