2015-11-04 31 views
1

我想製作一個提示用戶使用默認值的bat文件。沒關係,我找到了一種CScript方式。WScript SendKeys轉義特殊字符

但是我有特殊字符,如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(消息) ,在這裏我應該使用一個逃生功能,把鐲子放入特殊字符中。

有誰知道從SendKeys轉義郵件代碼的方法?

謝謝!

+0

批轉義字符是'^'(除非你轉義'%',那麼你使用第二個轉義它)。 – SomethingDark

+0

您的意思是?如果我添加)像WshShell.SendKeys(&GS {)} u **,o7,r)這樣的大括號將會像它應該那樣工作。因此需要一種使用SendKey方法的方法來使用轉義規則。 – oriceon

+1

以下是特殊字符的有效代碼:http://orporate.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx –

回答

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