user3023394
user3023394

Reputation: 13

Insert two batch commands inside a VBScript script

I have limited to no experience with VBScript scripts and could use some advice. We use this VBScript script to map network drives for a machine before it starts up an application that needs those drives. Now I also need to start two other external programs BEFORE the very last line of the script runs. One of these programs has special startup instructions.

[THIS IS THE TARGET FOR THE FIRST TO START ALONG WITH ITS CONFIGURATION] "C:\Program Files\Eterlogic Software\Free Virtual Serial Ports Emulator\VSPEmulator.exe" -minimize -hide_splash c:/config.vspe

[THIS IS THE NEXT THAT MUST START, BUT IT CANNOT START UNTIL THE ONE ABOVE DOES] "C:\Program Files\SAACUBridge\SAACUBridge.exe"

Those two must run RIGHT before the LAST line (StartNG.bat) of this VBScript script below.

===================================================

SCRIPT

option explicit
on error resume next
dim oShell, oNetwork

Set oShell=WScript.CreateObject ("WScript.Shell")

WScript.Sleep 15000 'Allow network services to start
set oNetwork=wscript.createobject ("wscript.network")
oNetwork.RemoveNetworkDrive "n:"
oNetwork.RemoveNetworkDrive "r:"
oNetwork.RemoveNetworkDrive "g:"
oNetwork.RemoveNetworkDrive "v:"
WScript.Sleep 10000
oNetwork.MapNetworkDrive "r:", "\\10.81.47.246\audio", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "n:", "\\10.81.47.246\SPOTS1", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "g:", "\\10.81.47.246\SONGS1", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "v:", "\\10.81.47.246\UPDATE", false, "NexGen", "7roppu$"

WScript.Sleep 5000
Dim oFS, fileBat
Set oFS = CreateObject("Scripting.FileSystemObject")
Set fileBat = Nothing
Set fileBat = oFS.CreateTextFile("c:\StartNG.bat", False)
if not fileBat is Nothing Then
   fileBat.WriteLine("v:\hlc\update.exe")
   fileBat.close
end if
oShell.Run "c:\StartNG.bat", 0, False

Upvotes: 1

Views: 803

Answers (4)

4cidwolf
4cidwolf

Reputation: 1

Change the following section:

 Dim oFS, fileBat  
Set oFS = CreateObject("Scripting.FileSystemObject")  
Set fileBat = Nothing  
Set fileBat = oFS.CreateTextFile("c:\StartNG.bat", False)  
if not fileBat is Nothing Then  
   fileBat.WriteLine("v:\hlc\update.exe")  
   fileBat.close  
end if  
oShell.Run "c:\StartNG.bat", 0, False  

To:

Dim oFS, fileBat  
Set oFS = CreateObject("Scripting.FileSystemObject")  
Set fileBat = Nothing  
Set fileBat = oFS.CreateTextFile("c:\StartNG.bat", False)  
if not fileBat is Nothing Then  
   fileBat.WriteLine("v:\hlc\update.exe")  
   fileBat.close  
end if  
oShell.Run """C:\Program Files\Eterlogic Software\Free Virtual Serial Ports Emulator\VSPEmulator.exe"" -minimize -hide_splash c:/config.vspe", 1, False  
WScript.Sleep 1000 'set sleep time in milliseconds for delay  
oShell.Run """C:\Program Files\SAACUBridge\SAACUBridge.exe""", 1, False  
WScript.Sleep 1000 'set sleep time in milliseconds for delay  
oShell.Run "c:\StartNG.bat", 0, False

The batch file is a workaround to help avoid Windows Open File Security Warning, since it executes a program on a mapped network drive.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You can run external commands from VBScript without writing them to a batch script first. Replace this:

Dim oFS, fileBat
Set oFS = CreateObject("Scripting.FileSystemObject")
Set fileBat = Nothing
Set fileBat = oFS.CreateTextFile("c:\StartNG.bat", False)
if not fileBat is Nothing Then
   fileBat.WriteLine("v:\hlc\update.exe")
   fileBat.close
end if
oShell.Run "c:\StartNG.bat", 0, False

with this:

oShell.Run """C:\Program Files\Eterlogic Software\Free Virtual Serial Ports Emulator\VSPEmulator.exe"" -minimize -hide_splash c:/config.vspe", 0, True
oShell.Run """C:\Program Files\SAACUBridge\SAACUBridge.exe""", 0, True
oShell.Run "v:\hlc\update.exe", 0, False

Since the paths of the first 2 commands contain spaces they must be put between double quotes. Because VBScript strings are delimited with double quotes as well, the inner double quotes must be escaped by doubling them, hence the notation:

"""C:\path to\some.exe"" -option"

Note: If the script should not wait for the first 2 programs to terminate (e.g. because they run as background processes) change the 3rd parameter from True to False. If needed you could also add some delay between the 3 Run calls by putting WScript.Sleep 1000 between them (adjust the number as you see fit).

oShell.Run """C:\Program Files\Eterlogic Software\Free Virtual Serial Ports Emulator\VSPEmulator.exe"" -minimize -hide_splash c:/config.vspe", 0, False
WScript.Sleep 1000
oShell.Run """C:\Program Files\SAACUBridge\SAACUBridge.exe""", 0, False
WScript.Sleep 1000
oShell.Run "v:\hlc\update.exe", 0, False

Upvotes: 0

Jobbo
Jobbo

Reputation: 1418

I would put the two commands you have:

"C:\Program Files\Eterlogic Software\Free Virtual Serial Ports Emulator\VSPEmulator.exe" -minimize -hide_splash c:/config.vspe
"C:\Program Files\SAACUBridge\SAACUBridge.exe"

At the beginning of c:\StartNG.bat and leave the VBScript as is

Upvotes: 1

Leptonator
Leptonator

Reputation: 3519

Something like this should do the trick:

option explicit
on error resume next
dim oShell, oNetwork

Set oShell=WScript.CreateObject ("WScript.Shell")

WScript.Sleep 15000 'Allow network services to start
set oNetwork=wscript.createobject ("wscript.network")
oNetwork.RemoveNetworkDrive "n:"
oNetwork.RemoveNetworkDrive "r:"
oNetwork.RemoveNetworkDrive "g:"
oNetwork.RemoveNetworkDrive "v:"
WScript.Sleep 10000
oNetwork.MapNetworkDrive "r:", "\\10.81.47.246\audio", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "n:", "\\10.81.47.246\SPOTS1", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "g:", "\\10.81.47.246\SONGS1", false, "NexGen", "7roppu$"
oNetwork.MapNetworkDrive "v:", "\\10.81.47.246\UPDATE", false, "NexGen", "7roppu$"

WScript.Sleep 5000
Dim oFS, fileBat
Set oFS = CreateObject("Scripting.FileSystemObject")
Set fileBat = Nothing
Set fileBat = oFS.CreateTextFile("c:\StartNG.bat", False)
if not fileBat is Nothing Then
   fileBat.WriteLine("v:\hlc\update.exe")
   fileBat.close
end if
' -- keep checking for the process running
Set Service = GetObject("winmgmts:\\.")
    for each Process in Service.InstancesOf ("Win32_Process")
        While Process.Name <> "PROCESS.exe"
        oShell.Run "c:\StartNG.bat", 0, False
            'wscript.echo "PROCESS running on " & ComputerName
        Wscript.sleep 2000 ' -- Pause for tw seconds, you may want run continually - but test carefully!!
    Next
Loop

Upvotes: 0

Related Questions