使用WshShell.Sendkeys和Windows批处理打印双引号

时间:2017-11-10 13:54:36

标签: windows batch-file parameters escaping wsh

我有一个脚本可以更改用户目录并使用命令预填充输入,因此他只需要查看它并在按Enter键之前更改值。要写的命令需要双引号,我无法在输入中打印它。我尝试了各种转义字符,如^",\","","""和{"}但是我没有让他们工作。我使用WshShell但是如果它不可能使用它,并且有更好的方法可以解决这个问题,那么我就是所有的耳朵。

目标是用户将在命令窗口中看到:

command.exe parameter="hello" parameter2="world"

.bat文件:

@if (@CodeSection == @Batch) @then
@echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start cmd.exe program 
start "" cmd
rem Send whatever you want to previous cmd.exe
%SendKeys% "echo Hello World!{ENTER}"
%SendKeys% "cd C:\temp{ENTER}"
%SendKeys% "command.exe parameter="hello" parameter2="world""
set /P "="
ECHO TERMINATE ORIGINAL BATCH
goto :EOF
@end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

batscript的来源:https://stackoverflow.com/a/22772040/1350598

1 个答案:

答案 0 :(得分:1)

使用%SendKeys% "'command.exe parameter=\u0022hello\u0022 parameter2=\u0022world\u0022'"并将最后一行更改为WshShell.SendKeys(eval(WScript.Arguments(0)));。请注意,您需要在所有字符串的开头添加撇号';那就是:

@if (@CodeSection == @Batch) @then
@echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start cmd.exe program 
start "" cmd
rem Send whatever you want to previous cmd.exe
%SendKeys% "'echo Hello World!{ENTER}'"
%SendKeys% "'cd C:\temp{ENTER}'"
%SendKeys% "'command.exe parameter=\u0022hello\u0022 parameter2=\u0022world\u0022'"
set /P "="
ECHO TERMINATE ORIGINAL BATCH
goto :EOF
@end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(eval(WScript.Arguments(0)));