Кто-нибудь знает, почему происходит следующее, и есть ли у кого-нибудь обходной путь?
Я пытаюсь захватить вывод команды mklink (через cmd.exe mklink> out.txt)
Вывод отправляется в out.txt в порядке, если команда mklink выполнена успешно.
E.G: %comspec% /c mklink /d C:\Test C:\Windows > out.txt && notepad out.txt
Однако, если команда недействительна или не удалась, в файл out.txt ничего не будет записано.
Например: Run above command again
(сбой, потому что C:\Test уже существует) или
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