How to hit Enter in cmd from VBSCript - 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}"

Related

how to start minecraft with vbs?

I'm working with a startup on my computer and I'm wondering how to start Minecraft with vbs.
Here is the code I have:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.sleep 200
wshshell.Run "chrome.exe"
WScript.sleep 200
wshshell.Run "start minecraft here"
WScript.sleep 200
WshShell.Run "C:\Users\username\Desktop\"
WScript.sleep 200
WshShell.Run "C:\Users\username\Desktop\Discord.lnk"
WScript.sleep 999
WshShell.Run "TASKKILL /F cmd.exe /T"
i only want it so it start Minecraft launcher. The problem for me is can't find it. I have try to start it with a shortcut some I have on the desktop. and if you see any more problems with my code so say that to, I'm new on vbs.
An easy workaround is simply to move the minecraft installation out from the Program Files (x86) folder to something that requires a bit less rights. This worked for me.

Simulating enter key in Command Prompt is not working

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

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)

Run external command silently

I am trying to run cmd code from vbscript (vbs file) silently.
I have tried this, but it doesnt hide the cmd window.
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /K ping example.org"
Set oShell = Nothing
What is the correct way to do that ?
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe ping example.org",0,True
Set oShell = Nothing
As per the MSDN for .Run you can use the optional parameter for intWindowStyle, which will hide most windows from the screen, by setting it to 0. The True is to tell the operation to wait until completion before completing the script. That is of course optional.
If you hide the window you need to remove /K or else the script will never complete.

VB Script to open multiple programs at once

Right im looking for a script that I can click on after I have logged in to open various programs just to save me a bit of time. I have managed to get a script to open one but as a bit of a newbie can someone provide advice.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe "" ""-
express:dvla.servicecenter.fs.fujitsu.com.12680"""
Set objShell = Nothing
You might be overthinking it a bit to use VBScript or Powershell for this job. A batch file will work.
#echo off
start "c:\Program Files\Folder 1\program.exe"
start "c:\Program Files\Folder 2\program.exe" -switch -argument
exit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("Path to program")
wscript.sleep (100)
objShell.Run("Path to program")
wscript.sleep (100)
wscript.quit
I do not have scguiw32.exe, so I created simple script which opens file in notepad and in word.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "C:\Windows\notepad.exe c:\dump.txt"
objShell.Run """C:\Program Files (x86)\Microsoft Office\Office14\winword.exe"" c:\dump.txt"
Set objShell = Nothing
BTW Instead of vbscript you can use now powershell, and powershell script is much more easy to understand. For example above one will be: Create run.ps1 with content
& 'C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE' c:\dump.txt
notepad.exe C:\dump.txt
Click it right-click and choose Run with Powershell
Here is how to use vbscript to create an array of programs you want to run and then execute each one.
'---Declare Variables
Dim objShell, strprogram1, strProgram2, colprograms, item
'---Create Scripting Shell Object
Set objShell = CreateObject("WScript.Shell")
'---Create Program Variables
strProgram1 = """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe"" ""-express:dvla.servicecenter.fs.fujitsu.com.12680"""
strProgram2 = "C:\Windows\notepad.exe C:\Dump.txt"
'---Add Variables to an Array
colPrograms = Array(strProgram1,strProgram2)
'---Run each program in the array once
For Each item In colprograms
objShell.Run item
Next
WScript.Echo "Done."

Resources