batch-file - Shell.StdOut.ReadAll 返回的垃圾字符

标签 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

最佳答案

您的问题的答案是 clsStdOut 流捕获并解释为扩展字符。摆脱它。你不需要它。

只是为了让我觉得我做了一些事情,这里是重写为批处理 + 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());

作为额外的奖励,这里添加了可爱的“叮”声。

@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());

关于batch-file - Shell.StdOut.ReadAll 返回的垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876525/

相关文章:

windows - 将 vbs 脚本的快捷方式固定到任务栏(W2008 服务器)

file - go - 将 stderr 重定向到文件和 stdout

python - 如何透明拦截stdin/out/err

bash - 在 Windows 批处理和 Linux Bash 中运行的单个脚本?

dll - 错误ASP 0177:8007007e Server.CreateObject对于COM DLL失败

java - ".bat"文件未将重音符号打印到 .txt 文件

vbscript - VBS发送鼠标点击?

python - 使用 tee 从 python 获取实时打印语句

windows - 检测确切的 Windows 10 版本以在批处理文件中回显彩色文本

windows - 如何使用 Windows 批处理脚本将文件独占锁定 1 分钟?