Shell.StdOut.ReadAll返回的垃圾字符

时间:2015-06-16 19:30:11

标签: batch-file vbscript stdout

这是我的代码

Set objFSO = CreateObject("Scripting.FileSystemObject")

a = "@echo off && " & _
"pushd ""\\xxxxx.local\cfs\Development\Docs\Baseline"" &&" & _
"cls &&" & _
"dir /b /a-d &&" & _
"popd"

Set objShell = CreateObject("WScript.Shell").Exec("%comspec% /c " & a)
execStdOut = objShell.StdOut.ReadAll()
msgbox execStdOut

基本上我正在尝试在网络驱动器上运行Dir命令。它工作正常,但唯一的问题是输出在开头包含一个垃圾字符。不确定为什么?

输出

如果我在批处理文件中运行相同的步骤,则输出正确

enter image description here

2 个答案:

答案 0 :(得分:5)

您的问题的答案是cls流正在捕获StdOut并将其解释为扩展字符。摆脱它。你不需要它。

就这样,我觉得我已经做了一些事情,这里的脚本被重写为批处理+ JScript混合。如果你想弄乱它,请用.bat扩展名保存。

@if (@CodeSection == @Batch) @then

@echo off
setlocal

if "%~1"=="shell" (
    pushd "\\xxxxx.local\cfs\Development\Docs\Baseline"
    dir /b /a-d
    popd
    goto :EOF
)

cscript /nologo /e:JScript "%~f0"

goto :EOF
@end // end batch / begin JScript

var oSH = WSH.CreateObject('Wscript.Shell'),
    proc = oSH.Exec("cmd /c " + WSH.ScriptFullName + " shell");

oSH.popup(proc.StdOut.ReadAll());

作为一个额外的奖励,这里有一个可爱的" ding"增加了噪音。

@if (@CodeSection == @Batch) @then

@echo off
setlocal

if "%~1"=="shell" (
    pushd "\\xxxxx.local\cfs\Development\Docs\Baseline"
    dir /b /a-d
    popd
    goto :EOF
)

cscript /nologo /e:JScript "%~f0"

goto :EOF
@end // end batch / begin JScript

var oSH = WSH.CreateObject('Wscript.Shell'),
    noise = WSH.CreateObject('WMPlayer.OCX.7'),
    proc = oSH.Exec("cmd /c " + WSH.ScriptFullName + " shell");

with (noise) {
    URL = oSH.Environment('Process')('SYSTEMROOT') + '\\Media\\Windows Exclamation.wav';
    Controls.play();
}

oSH.popup(proc.StdOut.ReadAll());

答案 1 :(得分:2)

字符是 ASCII控制字符换行符0x0A,是CLI cls命令的残余。只需在代码中省略"cls &&" & _行,如下所示:

a = "@echo off && " & _
  "pushd ""\\xxxxx.local\cfs\Development\Docs\Baseline"" &&" & _
  "dir /b /a-d &&" & _
  "popd"