Option Explicit
Dim oFSO, oTxtFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists("c:\users\Installed.txt") Then
WScript.Quit
Else
Set WshShell = CreateObject("WScript.Shell")
WshShell.Exec("\\1.1.1.1\software\1.exe -s /s -a ns=1.5.3.2")
Set oTxtFile = oFSO.CreateTextFile("c:\users\Installed.txt")
End If
My Code is above. I am getting an error when running my vbs. Line 13 char 1 variable is undefined. "WSHShell".
The goal is to have this vbs check for a file. If the file exists then end. If the file is not found run an installer with arguments, then create the file.
Any help would be great.
Thanks,
If you use "Option Explicit" - as you should - all variables must be (Re)Dim-ed. So add WshShell to you Dim statement.
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.
I'm trying to run a run the following command line for each specific file type (as example for each .txt file) in the current directory:
"C:\Program Files (x86)\some program\someprogram.exe" "file.txt" "file.txt.mod" -someparameter
When I run this exact command from an open Windows command prompt (including all the quotation marks), it works.
But when I run it through this VB, it does nothing/closes right away.
What am I doing wrong? I have a feeling it has to do with the quotes, but my head can't sort it out.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "txt" Then
RunCommand()
End If
Next
Sub RunCommand
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C ""C:\Program Files (x86)\some program\someprogram.exe"" """ & objFile.Path & """ """ & objFile.Path & ".mod"" -someparameter"
Set oShell = Nothing
End Sub
You should
Reduce risk of failures
by
using "Option Explicit"
avoiding clever "roll your own" hacks by using standard methods (.GetParentFolderName) instead
using type prefixes correctly (objStartFolder)
avoiding variables just used once (objFolder, colFiles)
not using globals to pass parameters into Subs/Functions (objFile)
avoiding (unnecessary) stress (.Run without wait, new WScript.Shell for each file, "cmd" instead of "%comspec%")
using cscript in a 'dos box' instead of double click/wscript
and
check your assumptions
by
diagnostic output (.Echo objFile.Name immediately before calling RunCommand, use a variable to store and .Echo the command send to .Run)
Check return values of functions that provide diagnostics (.Run)
sanity checks like:
(just to tame the formatter)
>> WScript.Echo goFS.GetExtensionName("A.TXT")
>>
TXT
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 am using the following code to call a batch file:
dim shell
set shell=createobject("wscript.shell")
shell.run "a.bat D:\a"
set shell=nothing
How do I call more than 1 batch file, so that when the 1st file's execution is over the 2nd file is executed.
as always, I really appreciate any help offered.
Below
shell.run "a.bat D:\a"
add another line with another
shell.run "b.bat ...."
Or create a batch file that calls all the other batch files, and call that batch file from your script.
Option explicit
Dim oShell
set oShell = Wscript.CreateObject("WScript.Shell")
oShell.Run "RunAs /noprofile /user:Admininistrator ""%comspec% /c 1stcommand && 2ndcommand && 3rdcommand""", 1, false
WScript.Sleep 1000
oShell.Sendkeys "AdmininistratorPassword~"
Wscript.Quit