cmd = "ipconfig"
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
outfile = fso.GetTempName
sh.Run "%COMSPEC% /c " & cmd & " 2>" & outfile & _
" 1>" & outfile, 0, true
当我执行命令" ipconfig "时,我有VBS代码的片段。我想从同一个.tmp文件中获取数据和错误线程的所有信息。 但执行此代码后,.tmp文件为空。 所以我无法理解为什么?
答案 0 :(得分:1)
尝试这样:
cmd = "ipconfig"
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
outfile = fso.GetTempName
sh.Run "%COMSPEC% /C " & cmd & ">" & outfile & " 2>&1", 0, true
sh.Run "Notepad " & outfile
编辑:其他要测试的代码
cmd = "Taskkill /F /IM iexplore.exe"
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
outfile = fso.GetTempName
sh.Run "%COMSPEC% /C " & cmd & ">" & outfile & " 2>&1", 0, true
sh.Run "Notepad " & outfile
答案 1 :(得分:0)
如果您尝试创建或覆盖TXT文件以管道输出,则使用大于符号的单个>
。
sh.Run "%COMSPEC% /c " & cmd & " 2>" & outfile & " 1>" & outfile, 0, true
如果要将管道输出附加到现有文件(或创建新文件),请使用两个大于符号的>>
。
sh.Run "%COMSPEC% /c " & cmd & " 2>>" & outfile & " 1>>" & outfile, 0, true