以下が発生する理由を知っている人はいますか?また、回避策はありますか?
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