Vbscript 捕获(失败)mklink命令输出

Vbscript 捕获(失败)mklink命令输出,vbscript,output,mklink,Vbscript,Output,Mklink,有人知道为什么会发生以下情况吗?有人有解决办法吗 我正在努力捕捉mklink命令输出(通过cmd.exe mklink>out.txt) 如果mklink命令成功,输出将发送到out.txt fine 例如:%comspec%/c mklink/d c:\Test c:\Windows>out.txt&¬epad out.txt 但是,如果命令无效或失败,则不会向out.txt写入任何内容 例如:再次运行上述命令(由于C:\Test已存在而失败)或 例如:%comspec%/c mklin

有人知道为什么会发生以下情况吗?有人有解决办法吗

我正在努力捕捉mklink命令输出(通过cmd.exe mklink>out.txt)

如果mklink命令成功,输出将发送到out.txt fine

例如:
%comspec%/c mklink/d c:\Test c:\Windows>out.txt&¬epad out.txt

但是,如果命令无效或失败,则不会向out.txt写入任何内容

例如:
再次运行上述命令
(由于C:\Test已存在而失败)或

例如:
%comspec%/c mklink>out.txt和¬epad 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

您是否考虑过使用“Exec”命令而不是run命令来收集输出结果

它不需要文件,而且更简单

新代码

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 
旧代码

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 
(1) 根据,符号
&&
仅在左侧的命令成功时才运行右侧的命令。您必须使用
&
即使
mlink
失败,也要启动记事本

(2) 虽然没有明确说明,但我假设
mlink
将其错误消息写入Stderr(请参阅)-就像
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
(rsp.
dir
)的输出(无论是否失败),并在记事本中显示结果(文件),必须使用

dir 01.vbs 1> out.txt 2>&1 & notepad out.txt
dir nix 1> out.txt 2>&1 & notepad out.txt
将Stdout和Stderr重定向到输出文件

证据:

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 &)

我以前尝试过使用Exec,甚至添加了一个循环来等待命令状态,但结果总是一样的,就像我运行这个:OK
GetCommandOutput=oExec.StdErr.ReadAll
works:)问题解决了,Sweet也解决了-干杯!我只是在使用&¬epad尝试打开输出文件来向大家展示,但再次感谢使用commands链接的提示+1。我不太明白你有时在回答中使用的证据。我知道这是代码执行的实时日志。我只是看不出有什么解释。