Simulating enter key in Command Prompt is not working - vbscript

Set WshShell = CreateObject("WScript.shell")
WshShell.Run "cmd /K cd /d D:\Path\&abcd.bat"
wait 5
WshShell.SendKeys "{ENTER}"
The line
WshShell.Run "cmd /K cd /d D:\Path\&abcd.bat"
is working fine, runs the abcd.bat file (runs the file, then command prompt asks to press the enter key), but pressing Enter
WshShell.SendKeys "{ENTER}"
is not working. QTP/UFT is not doing anything and skips this line of code and moves to next line.
I tried
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "~"
WshShell.SendKeys "(Chr(13))"
Nothing worked.

Try
WshShell.SendKeys "~"
Sometimes this works where {ENTER} doesn't. Don't ask me why.

Instead of initializing an 'WScript.Shell', you could use the standard functionality of UFT/QTP:
SystemUtil.Run "D:\Path\To\batch.bat"
Have an look at the official SystemUtil Reference: SystemUtil Object

Related

vbs key press script

I am trying to write a script in vbs that will press Ctrl+Shift+R
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Summit.Scripting.Toolkit.exe"
wscript.sleep 2000
WshShell.SendKeys "+^r"
So far I was able to get the script to activate/select the application but unfortunately the key presses do not seem to work
Referencing another website I see Ctrl key is ^
and Shift key is +
and r is r
but it doesn't seem to run the combination correctly
5/25/18
I made some changes to my code, I seem to be getting closer to figuring it out.
#echo off
forfiles -p "C:\Program Files (x86)\SummitHealthcare\CommonFiles\TextOut" -s -m *.log /D -0 /C "cmd /c del #path"
wscript.sleep 2000
cd "C:\Program Files (x86)\SummitHealthcare\Scripting Toolkit 7\Projects"
start Single_BARCommentsDBwithLoop23.ssc
wscript.sleep 1000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "{F10}"
wscript.sleep 2000
WshShell.SendKeys "{r}"
I have the code delete old cache files for the application, Then the script waits, Opens up the application then waits again
then makes the keystrokes F10 and r to start the application.
For some reason the keystrokes do not seem to be registering.
I've Tried separating the key strokes to its own script and it works fine.
I've tried making the wait longer.
I've also tried adding the code below to select and make sure the application is active. Adding this code seems to break the entire code.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Summit.Scripting.Toolkit.exe"
Some code must be conflicting.
If you're going to press the "r" key, you should use this code:
wscript.sleep 2000
cd "C:\Program Files (x86)\SummitHealthcare\Scripting Toolkit 7\Projects"
start Single_BARCommentsDBwithLoop23.ssc
wscript.sleep 1000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "{F10}"
wscript.sleep 2000
WshShell.SendKeys "r"#echo off
forfiles -p "C:\Program Files (x86)\SummitHealthcare\CommonFiles\TextOut" -s -m *.log /D -0 /C "cmd /c del #path"
wscript.sleep 2000
cd "C:\Program Files (x86)\SummitHealthcare\Scripting Toolkit 7\Projects"
start Single_BARCommentsDBwithLoop23.ssc
wscript.sleep 1000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "{F10}"
wscript.sleep 2000
WshShell.SendKeys "r"
You're sending the key stroke as a character, so you would want to press "r" rather than "{r}"
Now, I did start learning/experimenting with VBS like 3 days ago, but I think that this code would work a bit better.
I had a similar question: VBScript sendkeys, Trying to do CTRL+ALT+A
The only things I could find is to use a powershell script that send the keys combination and run it from your VBScript.
The VBS "WshShell.SendKeys" doesn't work every time. For the example in the link, when I was using the command, it worked like 1 time out of 4 or 5 (it's not reliable)

notepad loop created automatically with VBScript

I've created a VBScript to open notepad and write Hello. But it is opening notepad again and again
my code is:
WScript.Sleep 1000
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad"
WScript.Sleep 100
WshShell.AppActivate "Notepad"
WScript.Sleep 500
WshShell.SendKeys "Hello"
WScript.Sleep 500
Did you name your script notepad.vbs? please change the name, or
WshShell.Run "notepad.exe"
If no full path specified, windows will try to find the program in the working directory first instead of %PATH%.

Batch File to call VBS which sends keys to application

So I am trying to make a batch file that will download your search history. In doing this I need to use the keys Ctrl+A and Ctrl+S along with typing the name of the file. I just need to find a way to have the code automatically carry out the action of selecting all the history by Ctrl+A and saving it by Ctrl+S and then naming it scarra history.
I have a folder containing 4 files:
Scarra.bat
ChromeHistoryView.exe
Sendkeys.bat
Sendkeys.vbs
Scarra.bat is as follows:
call ChromeHistoryView.exe
call sendkeys.bat
pause
Sendkeys.vbs is as follows:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^a"
Sendkeys.bat is as follows, this is what I use to try to call the bat:
wscript "sendkeys.vbs"
Add this to Sendkeys.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^a"
WScript.Sleep 1000
WshShell.SendKeys "^s"
WScript.Sleep 2000
WshShell.SendKeys "scarra history"
Change Sendkeys.bat to
cscript sendkeys.vbs

How to hit Enter in cmd from VBSCript

Trying to run few Commands from VBScript
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c cd C:\Script & lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property"
The Second command lcm_cli.bat -lcmproperty C:\LCMBiar_Import.property requires me to hit enter to complete its execution. Any idea how to do that through VBScript.
Tried giving
objShell.Sleep(1000)
objShell.SendKeys("{ENTER}")
but nothing happens. Still waits for manual hit of enter!
The solution is:
WshShell.SendKeys "{ENTER}"

How Can i open an cmd prompt and set an path to open an .bat file in Vb Script..?

I need to Automate the Whole Process, So the Cmd Prompt opens and path is set there for the .bat file to open
I have tried with Window Shell Commands to do this .. they work fine .. But i need some other way to open and set the path to run the file ..can anyone help me
Thanks In Advance
Here you go
Set WshShell = wscript.CreateObject("wscript.Shell")
WshShell.Run "cmd"
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 100
wshshell.sendkeys "c:\path"
wshshell.sendkeys "{ENTER}"
wshshell.sendkeys "SET PATH=%PATH%;c:\tmp"
wshshell.sendkeys "{ENTER}"
wshshell.sendkeys "batch.cmd"
wshshell.sendkeys "{ENTER}"

Resources