Declare VBScript inside of VBScript (Running multiple .VBS at a time) - vbscript

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.

Related

Delete file within APPDATA folder

I have many PCs that currently have a Personal macro workbook installed. More specifically, they all have a shortcut to a Personal macro workbook on a network drive.
To install that, I went to each PC and ran this VBScript:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
oFSO.CopyFile "H:\Folder\Folder\Folder\PERSONAL 1.xlam - Shortcut.lnk", strAppData, True
Set objShell = Nothing
Set oFSO = Nothing
Now though, I want to remove that shortcut to PERSONAL 1.xlam from the XLSTART folder and copy over a shortcut to a different macro workbook.
This might be really easy, but I'm new to VBS and I haven't found a way to delete a file without having the the exact path. And since the path is going to be unique to each PC, I can't do that here.
You just need to modify one line from the above script. Try the following:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
'here is the modified line
oFSO.DeleteFile strAppData & "PERSONAL 1.xlam - Shortcut.lnk", True
Set objShell = Nothing
Set oFSO = Nothing

Restart vb script

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.

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

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

How to execute a vbscript from another vbscript?

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

Resources