I'm trying to make a Vb script that restarts another vbs script the problem is I'm new to this and I don't know how to do this, someone suggested using WshShell I have tried a few websites on how to use it but nothing. Here is what I've got,
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("Test_To_Block.vbs")
Do
If NOT WshShell.Status = 1 then
WScript.Exec("Test_To_Block.vbs")
End If
WScript.Sleep(100)
Loop
Thanks,
Regards,
A Viper
Yes, you can use Exec method to run another VB Script, but you are likely to get a console window flash.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Exec("CMD /C Test_To_Block.vbs")
Refer to SS64 site to learn about VB Script basics.
Related
I need to run multiple .vbs at a time. This is the only way I was able to find online:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "1.vbs"
objShell.Run "2.vbs"
objShell.Run "3.vbs"
Set objShell = Nothing
To do that I'd have create 1.vbs, 2.vbs and 3.vbs separately. Is there a way to input them all as part of one .vbs file? Something like
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim vbs1 as vbscript
Dim vbs2 as vbscript
Dim vbs3 as vbscript
Set vbs1 =
"Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\test1.xlsm'!Module1.refresh"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing"
Set vbs2 = ' whatever code
Set vbs3 = ' whatever code
objShell.Run vbs1
objShell.Run vbs2
objShell.Run vbs3
Set objShell = Nothing
The purpose of this:
I have ~50 excel reports with connections to SQL that need to be updated every day.
To do that I've created a macro and added it to each of them. The macro refreshes connections/queries > refreshes pivot tables > removes the connections/queries > saves as a macro-free workbook in a specified location.
I wrote .vbs scripts for each report. It just runs that macro in every Excel workbook.
Instead of running every .vbs separately, I've created one main .vbs that references all prior created .vbs to run them at the same time.
My question is if there's a way to do that without creating 50 separate .vbs files.
Is it possible to execute a remote executable using a .vbs script? My below attempt doesn't seem to work (sadly).
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("""\\12.345.67.789\filename.exe""")
Set objShell = Nothing
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."
For example, if I have two vbscript files: A.vbs and B.vbs. I would like the vbscript in B.vbs to execute A.vbs, such peudo-code would look like the following:
'B.vbs
execute("A.vbs")
Just as simple as this line, but I couldn't really find a valid syntax to accomplish such task. Hope someone could help me out, thanks.
Dim oShell
Set oShell = Wscript.CreateObject("WScript.Shell")
oShell.Run "name_of_vbs_file_here.vbs"
Set oShell = Nothing
The following will execute a.vbs as it were a part of the calling script itself
include "a.vbs"
sub include(script)
dim fso, file
set fso = createObject ("Scripting.Filesystemobject")
if fso.FileExists (script) then
set file = fso.OpenTextFile (script)
executeGlobal file.ReadAll ()
file.Close
set file = nothing
end if
set fso = nothing
end sub
Dim Shell
Set Shell = CreateObject ("WScript.Shell")
Shell.Run "a.vbs"
You can also spice it up a little by adding things like "SendKeys" or other Shells.
createobject("wscript.shell").run"a.vbs"
or if your files aren't in the same folder
createobject("wscript.shell").run"""C:\Users:\User:\YourFolder\a.vbs"""
I know how to execute a exe with wscript.exe, something like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\myprogram.exe"
But I'm forced to run my vbscript with cscript.exe, and can't use the WScript object. Are there any way to execute an exe when loaded with cscript.exe?
Both wscript.exe and cscript.exe provide the WScript object; so "Set WshShell = WScript.CreateObject(...)" is okay for .vbs files started with "w|cscript.exe whatever.vbs". VBScript - the language - provides its own CreateObject() function, so you can use plain "Set WshShell = CreateObject(...)" in all scripts (.hta, html too). The WScript COM object is another object. You can use it 'everywhere' (if we disregard security settings). In short: your code will work (or fail) with both hosts.
Set objShell = CreateObject("WScript.Shell")
objShell.run("cscript d:\Test2.vbs")