How-to: Use Hybrid Batch and VBscript

If you need to run both a batch file and a VBScript, the most simple method is to have, two separate files and run one from another, but that does require both the scripts to be in the same folder (or in some known location)

A hybrid script is a single script containing both batch commands and VBscript commands, the way this normally works is to use redirection to create a temporary vbscript file.

For example to generate a one line VBScript containing wscript.echo "Hello world", you can do
Echo wscript.echo "Hello world" > %temp%\~hi.vbs
cscript //nologo %temp%\~hi.vbs

The variable %temp% will expand to the users temporary files folder.

Writing longer hybrid scripts can involve a lot of Echo and redirection commands, one for every line, so there are several techniques which can be used to make this less verbose, using variables to hold the repeated commands/options, or as below using a comment ('VBS) at the end of every VBScript line and then using Findstr to extract the VBScript.
The expression %~f0 resolves to the full path of the batch file, so this allows the script to search itself:

@Echo off
Setlocal
Echo This is a Batch file
FINDSTR /E "'VBS" "%~f0 >%temp%\~temp.vbs
cscript //nologo %temp%\~temp.vbs
Del %temp%\~temp.vbs
Echo All Done.
EXIT

Sub Demo 'VBS
   wscript.echo "Welcome to VBScript" 'VBS
End Sub 'VBS

demo 'VBS
wscript.quit 0 'VBS

“I've actually made a prediction that within 30 years a majority of new cars made in the United States will be electric. And I don’t mean hybrid, I mean fully electric” ~ Elon Musk

Related VBScript commands

Hybrid scripts without a temporary file - StackOverflow
Run a script from VBScript
Run a script from PowerShell
Run a script from the CMD shell


 
Copyright © 1999-2024 SS64.com
Some rights reserved