vbs run program with objShell.run - vbscript

I have the following vbscript that fail with an error on last line
Option Explicit
Dim objShell
Dim strComputer, strCmd , strVar
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
' strVar = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
' strCmd = strVar & "\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
' wscript.echo strCmd
objShell.Run "taskkill /im Autobites.exe",,True
WScript.Sleep(5000)
objShell.run """%ProgramFiles(x86)%""" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
The problem is that script open the folder %ProgramFiles(x86)% but don't run the exe autobites.exe
Can you help me to debug it

The whole file spec has to be quoted, not only the first part:
objShell.run """%ProgramFiles(x86)%" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"""

Related

rename a file using VBScript, launch file, wait, and rename again

I need to create a vbs script (for maintenance purposes) that renames foo.txt to a foo.bat and launch foo.bat and when foo.bat ends, rename foo.bat again to foo.txt
This is my script vbs:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, vbHide, true
Fso.MoveFile "foo.bat", "foo.txt"
On Error GoTo 0
the script executes well. Rename foo.txt to foo.bat. Launches foo.bat, but does not expect foo.bat to end and renames it to foo.txt.
I changed this line, nothing happens:
objshell.Run NewPath, vbHide, 1, true
What do I need or what did I do wrong?
Alternative Solution (no VBScript): (By suggestion of #KenWhite)
code:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, true
On Error GoTo 0
And at the end of foo.bat:
ren foo.bat foo.txt
exit
Thanks
Here is a possible solution just in case anyone is wondering how to solve this problem without resorting to the alternate proposal mentioned above.
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run "%COMSPEC% /c " & NewPath, 1, true
' Changes start here
'===================================================================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Hold execution until cmd.exe process is done
do
' Get cmd.exe processes
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE 'cmd.exe'")
WScript.Sleep 250
Loop while colProcessList.count > 0
Fso.MoveFile "foo.bat", "foo.txt"

If - Else If statement in VBScript

Else If exist "K:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "K:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "F:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "F:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "E:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "E:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "D:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "D:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "C:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "C:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
What is wrong with the code? And how well the If statement is used? please help me.
How can this code be improved?
To search for a program on different drives and call it (perhaps with arguments and decent quotes), use a loop over the drives:
Option Explicit
Const csPrg = "pipapo"
Function qq(s) : qq = """" & s & """" : End Function
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sDL, sFSpec, sCmd
For Each sDL In Split("A:\ C:\ K:\ E:\")
sFSpec = goFS.BuildPath(sDL, csPrg)
sCmd = Join(Array("%comspec% /c", qq(sFSpec), "/p:arm", qq("one two three"), 4711))
WScript.Echo sCmd
If goFS.FileExists(sFSpec) Then Wscript.Echo " ==> now run sCmd (and perhaps Exit For)"
Next
output:
cscript 47922850.vbs
%comspec% /c "A:\pipapo" /p:arm "one two three" 4711
%comspec% /c "C:\pipapo" /p:arm "one two three" 4711
%comspec% /c "K:\pipapo" /p:arm "one two three" 4711
%comspec% /c "E:\pipapo" /p:arm "one two three" 4711
==> now run sCmd (and perhaps Exit For)

Build path and use in external command

How to insert my SCRIPT_PATH Variable into PowerShell script path? Also anyone know any Visual Studio Like tool I can write those? I think Visual Studio uses different .net vbs.
Here is the code:
SCRIPT_PATH = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - (Len(WScript.ScriptName)))
MsgBox SCRIPT_PATH
Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS /user:Domain\User ""powershell SCRIPT_PATH & Delete_App_Script.ps1"""
Set objShell = Nothing
How do I do SCRIPT_PATH = SCRIPT_PATH + Delete_App_Script.ps1?
SCRIPT_PATH = SCRIPT_PATH & "Delete_App_Script.ps1" doesn't work.
It gives no errors, so I am not sure what is the problem.
Am I missing some "" or some ,,?
I've seen some .Run ,,, syntax for Admin running and this one, doesn't explain what goes into "".
Not looking forward creating interface for this.
This doesn't work:
Set objShell = CreateObject("WScript.Shell")
SCRIPT_PATH = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - (Len(WScript.ScriptName)))
SCRIPT_PATH = SCRIPT_PATH & "Delete_App_Script.ps1"
objShell.Run "RUNAS /user:Dom\adm ""powershell SCRIPT_PATH"""
Set objShell = Nothing
But this works:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS /user:Dom\adm ""powershell C:\Delete_App_Script.ps1"""
Set objShell = Nothing
What am I missing?
Use the appropriate FileSystemObject methods when handling paths:
Set fso = CreateObject("Scripting.FileSystemObject")
SCRIPT_PATH = fso.GetParentFolderName(WScript.ScriptFullName)
SCRIPT_PATH = fso.BuildPath(SCRIPT_PATH, "Delete_App_Script.ps1")
For using variables inside strings you need to concatenate the variables to the rest of the string, as #Lankymart pointed out. This is also documented in the language tag info.
objShell.Run "RUNAS /user:Domain\User ""powershell " & SCRIPT_PATH & """"
Note also that you should put the path in double quotes in case it contains spaces:
objShell.Run "RUNAS /user:Domain\User ""powershell \""" & SCRIPT_PATH & "\"""""
and you should use the parameter -File, otherwise PowerShell would interpret the script path as a command string, which wouldn't fail for paths with spaces.
objShell.Run "RUNAS /user:Domain\User ""powershell -File \""" & SCRIPT_PATH & "\"""""

Run a vbs script from another vbs script and redirect right away output to a file

Running a vbscript from another vbscript, Is it possible to get right away an output to a file like this one:
dim shell
set shell=createobject("wscript.shell")
strCMD =replace("'myvbs.vbs' '"&a_parameter&"' ","'","""")
shell.Run strCMD >output.txt
Thank in advance
Yes, you can. Try this example.
'script1.vbs -----------------
Dim oShell, strCMD
Set oShell = CreateObject("WScript.Shell")
strCMD = Replace("CMD /C CScript.exe 'script2.vbs' " & 3, "'", """")
oShell.Run strCMD & " //NoLogo >output.txt", 0, True
'script2.vbs -----------------
result = WScript.Arguments(0) ^ 2
WScript.StdOut.Write result
Take a look at this answer too.

VBS Script - Run series of .batch jobs

Help me run a series of .bat script
they are located like so:
p:\Co-Brand\export.bat
p:\Generic\export.bat
p:\Tri-Brand\export.bat
Thanks in advance,
Best regards,
Joe
Would a simple shell command do? You can call this from a command prompt:
for /R %F in (*.bat) do "%F"
or the following from a .bat file:
for /R %%F in (*.bat) do call "%%F"
found a way that works, should have tried this first of all.
I am a bit embarrassed that it was this easy actually:
cd P:\Co-Brand\
CALL Export.bat
cd P:\Generic\
CALL Export.bat
cd P:\TriBrand\
CALL Export.bat
cd P:\UBA\
CALL Export.bat
As originally asked, here is a VBScript solution...
The problem described is probably related to the "Script-Working-Directory".
Try this ...
Dim objShell
Dim blnWaitOnReturn
Dim strOriginalCD
Dim strCmd
Dim intWindowStyle
Dim intExitCode
Set objShell = WScript.CreateObject("Wscript.Shell")
'' if necessary, save the original "Script-Working-Directory"
strOriginalCD = objShell.CurrentDirectory
intWindowStyle = 1
blnWaitOnReturn = True
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
intExitCode = objShell.Run(strCmd, intWindowStyle, blnWaitOnReturn)
'' if necessary, restore the original "Script-Working-Directory"
objShell.CurrentDirectory = strOriginalCD
Notes:
'' If filename contains spaces make sure to add double-quotes around filename
strCmd = "%comspec% /K " & Chr(34) & "File name with spaces.bat" & Chr(34)
'' To run the commands in a "Hidden" window, use:
intWindowStyle = 0
'' To run the commands "Minimized", use:
intWindowStyle = 7
More info on "objShell.Run" can be found here: http://ss64.com/vb/run.html
The above examples will cause VBScript to wait for each called ".bat" to complete and return an "ExitCode" before proceeding.
If you don't want VBScript to wait for one ".bat" to complete before proceeding to the next then set blnWaitOnReturn = False, and remove intExitCode like:
...
blnWaitOnReturn = False
objShell.CurrentDirectory = "p:\Co-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Generic\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
objShell.CurrentDirectory = "p:\Tri-Brand\"
strCmd = "%comspec% /K export.bat"
objShell.Run strCmd, intWindowStyle, blnWaitOnReturn
...
If you want the ability to get the "Status" and "ProcessID", and access the standard streams of the executable to read/write to the process's stdout/stderr in real-time while the process executes, then use "objShell.Exec".
More info on "objShell.Exec" can be found here: http://ss64.com/vb/exec.html

Resources