Re: Call a batch file from a vbs script

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 03/15/05


Date: Tue, 15 Mar 2005 10:31:23 +0100

SpiritBoy wrote:

> I am new to vbs and need some help. Can anyone give me a sample
> of how you can call a batch file from within a .vbs script?
> This is going to be used in a workgroup enviroment.
Hi

I prefer to use the Run method:

'--------------------8<----------------------

sBatFile = "c:\Scripts\something.bat"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FileExists(sBatFile) Then
    MsgBox "Could not find batch file, quitting!", _
       vbCritical + vbSystemModal, "Text search"
    WScript.Quit
End If

' just in case there is spaces in the path
sBatFileShort = oFSO.GetFile(sBatFile).ShortPath

' Run the batch file hidden, and let the VBScript wait for
' the batch file to finish
oShell.Run sBatFileShort, 0, True

'--------------------8<----------------------

If there is a space in the bat, an alternative to use the
sBatFileShort:

oShell.Run """" & sBatFile & """", 0, True

(In VBScript, inside a quoted string, use two quotes to get one
effective)

WSH 5.6 documentation (local help file) can be downloaded
from here if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp

-- 
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx


Relevant Pages

  • Re: Move n files sort by Filename
    ... It's not VBScript ... This may be true, however, I agree with mayayana that, when posting a solution based on a language other than the one on which this newsgroup is based, you should put your OT response in a proper context. ... *** In a batch file a file sort is effected with a simple ... not some alternative scripting language. ...
    (microsoft.public.scripting.vbscript)
  • Re: Robocopy - not providing destination in log output
    ... command line tool to be a part of that. ... working script (Batch file) servicing a large ... People who post questions in a *scripting* newsgroup ... Function pad ...
    (microsoft.public.windows.server.scripting)
  • Re: Writing a batch file
    ... Sorry, I can't, VB Scripting is not my area of expertise. ... Paul Randall will respond, or else you could post your question ... by the way thank you so much for your answer about my batch file. ... Prompt commands. ...
    (microsoft.public.windowsxp.general)
  • Re: script to create a file with unique name
    ... > Or if you are using a batch file, you could use the code below. ... > echo File name: %today%.txt ... > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.windows.server.general)
  • Re: Email ntbackup result and report
    ... Can you just put that coding into a batch file and it wil work? ... SAPIEN Technologies - Scripting, Simplified. ... VBScript & Windows PowerShell Training - ... blog: http://jdhitsolutions.blogspot.com ...
    (microsoft.public.windows.server.scripting)