Run external command silently - vbscript

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.

Related

How to run a cmd file using vbscript? [duplicate]

I need to run a command to copy a file from one location to another through Command Prompt using a vbs file. this is what I have however it keeps throwing an error at me.
'Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C copy "S:Claims\Sound.wav" "C:\WINDOWS\Media\Sound.wav"
Set oShell = Nothing'
The error i get is:
'Script: C:\******\command.vbs
Char: 30
Error: Expected end of statement
Code: 80040401
Source: Microsoft VBScript compilation error'
Please help :)
The problem is on this line:
oShell.run "cmd.exe /C copy "S:Claims\Sound.wav" "C:\WINDOWS\Media\Sound.wav"
Your first quote next to "S:Claims" ends the string; you need to escape the quotes around your files with a second quote, like this:
oShell.run "cmd.exe /C copy ""S:\Claims\Sound.wav"" ""C:\WINDOWS\Media\Sound.wav"" "
You also have a typo in S:Claims\Sound.wav, should be S:\Claims\Sound.wav.
I also assume the apostrophe before Dim oShell and after Set oShell = Nothing are typos as well.
Set oShell = CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C copy ""S:Claims\Sound.wav"" ""C:\WINDOWS\Media\Sound.wav"" "

invalid window handle with error code : 80070578

I have written below vb script to find an window and bring it to focus
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'bring the window to front
'title must be exactly what you see in the titlebar of the window
oShell.AppActivate WScript.Arguments(0)
I am calling this vb script from java code :
Runtime.getRuntime().exec("cmd.exe /c activeWindow.vbs mywindowname");
Now sometimes it is throwing this below exception :
This is happening randomly.
Syntax for AppActivate :
objShell.AppActivate strApplicationTitle
Below code is working directly by passing title name
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate "Calculator"
Or Passing as argument "WScript.Arguments(0)". Save this code in VBS file
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate WScript.Arguments(0)
WScript.Quit 0
from cmd type wscript Sample.vbs Calculator
Code is working smoothly and Calculator screen is activating .

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}"

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."

why WScript.CreateObject ("WScript.Shell") should crash?

Do you think of any reason that this line should crash:
Set oShell = WScript.CreateObject ("WScript.Shell")
... only if I launch the script from InstallShieldExpress as a custom-action.
MsgBox "before create ObjectShell"
Set oShell = WScript.CreateObject ("WScript.Shell")
MsgBox "after create ObjectShell"
I never see the "after create ObjectShell" message ;-(
and if I simply launch the script by double-clicking on the script file in a windows explorer, of course everything is ok.
It may be that the global WScript object isn't available in the InstallShield environment. You can check this using a script like this:
MsgBox Not IsEmpty(WScript) ' True if WScript is defined, False if it's undefined
If WScript is undefined, try using CreateObject("WScript.Shell") instead. See also What is the difference between CreateObject and Wscript.CreateObject?

Resources