WScript SendKeys转义特殊字符

时间:2015-11-04 22:36:57

标签: batch-file wsh

我想制作一个提示用户使用默认值的bat文件。一切都很好,我发现了CScript方式。

但我对像pharanteses这样的特殊字符有问题。

bat文件是

set "mysql_password="
title MyUniqueTitle
CScript //E:JScript //Nologo "%cscripts_path%\sendkeys.js" "MyUniqueTitle" "&GS)u**,o7,r"
set /p "mysql_password=> Enter new MySQL Password: "

和sendkeys.js

try
{
    var WshShell = WScript.CreateObject('WScript.Shell');
    var Title = WScript.Arguments.Item(0);
    var Message = WScript.Arguments.Item(1);
    WshShell.AppActivate(Title);
    WshShell.SendKeys(Message)
    WScript.Quit(0);
}
catch(e)
{
    WScript.Echo(e);
    WScript.Quit(1);
}
WScript.Quit(2);

问题在于WshShell.SendKeys(Message),在这里我应该使用一个将大括号添加到特殊字符的转义函数。

有没有人知道从SendKeys转义消息代码的方法?

谢谢!

1 个答案:

答案 0 :(得分:1)

首先在批处理文件中使用字符串替换来转义特殊字符。

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

@echo off &setlocal enabledelayedexpansion

set "password=&GS)+[]+u**,o7,r"

SET "password=!password:)={)}!"
SET "password=!password:+={+}!"
SET "password=!password:[={[}!"
SET "password=!password:]={]}!"

rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "!password!"
rem Read the variable
echo -----------------------------------------------------------
set /P "password=Please enter your password: "
echo password=!password!
pause
goto :EOF

@end

WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));