Pressing a key multiple time in batch file

Viewed 42

I have a script which is used to insert data in a web form.There is some data in the form field initially, before inserting the new data i want to clear the field so that i can type new data.To achieving this I am using the multiple backspace.Is there any method by which i can press any key multiple time instead of writing the same code in multiple lines. Or any syntax which can simulate Ctl+A key which is used to select all.My code goes as follows

@if (@CodeSection == @Batch) @then
@echo off
setlocal enabledelayedexpansion
set SendKeys=CScript //nologo //E:JScript "%~F0"
START msedge "https://fams.nabard.org:48443/PanaceaFAMS/login.faces"

timeout /t 6
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "{BACKSPACE}"
!SendKeys! "43569"
!SendKeys! "{TAB}"  
@end
// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
1 Answers

Perhaps this idea would make more sense to you. It should send each key as you've shown, but does so by executing CScript only once, which is much more efficient:

<!-- :
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "LURL=https://fams.nabard.org:48443/PanaceaFAMS/login.faces"
Set "Keys={BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} 43569 {TAB}"

Set "Edge=%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
Set "Wait=%SystemRoot%\System32\timeout.exe"
Set "CScr=%SystemRoot%\System32\cscript.exe"

Start "" "%Edge%" "%LURL%"
%Wait% /T 6 /NoBreak 1>NUL
%CScr% //NoLogo "%~f0?.wsf" %Keys%
Exit /B
-->
<Job><Script Language="VBScript">
    Set objArgs= WScript.Arguments
    Set objShell = WScript.CreateObject("WScript.Shell")
    For Each strArg In objArgs
        objShell.SendKeys "" & strArg & ""
    Next
    </Script></Job>

Note, I have used VBScript above, as opposed to your chosen JScript. To do it using JScript, you could do it similarly:

<!-- :
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "LURL=https://fams.nabard.org:48443/PanaceaFAMS/login.faces"
Set "Keys={BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} 43569 {TAB}"

Set "Edge=%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
Set "Wait=%SystemRoot%\System32\timeout.exe"
Set "CScr=%SystemRoot%\System32\cscript.exe"

Start "" "%Edge%" "%LURL%"
%Wait% /T 6 /NoBreak 1>NUL
%CScr% //NoLogo "%~f0?.wsf" %Keys%
Exit /B
-->
<Job><Script Language="JScript">
    var objShell = WScript.CreateObject("WScript.Shell");
    objArgs = WScript.Arguments
    for (i=0; i<objArgs.length; i++) {objShell.SendKeys(objArgs(i))};
    </Script></Job>

Now to answer your specific question, you should be able to replace your nine 'Backspace' keystrokes with a single 'Clear the field' keystroke.

You'll see that I already replaced your {BACKSPACE} with {BS}, (although {BKSP} works too, if you prefer), so just change:

Set "Keys={BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} {BS} 43569 {TAB}"

to:

Set "Keys={CLEAR} 43569 {TAB}"
Related