【问题标题】:WScript SendKeys escape special charactersWScript SendKeys 转义特殊字符
【发布时间】:2015-11-04 22:36:57
【问题描述】:

我想制作一个 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 中转义消息代码的方法?

谢谢!

【问题讨论】:

  • 批量转义字符为^(除非您要转义%,否则您使用第二个转义字符)。
  • 什么意思?如果我在 WshShell.SendKeys(&GS{)}u**,o7,r) 等大括号中添加 ) 将正常工作。所以需要在 SendKey 方法中使用转义规则。
  • 这里是特殊字符的有效代码social.technet.microsoft.com/wiki/contents/articles/…
  • @oriceon 执行 cscript 时为什么不直接转义它。 CScript //E:JScript //Nologo "%cscripts_path%\sendkeys.js" "MyUniqueTitle" "&GS{)}u**,o7,r"
  • 因为它是动态密码。使用的一个是例如目的:|

标签: batch-file jscript wsh


【解决方案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));

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2019-01-29
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多