How to Embed VBScript in cmd/bat script (VBscript vs. Batch DOS)

Send Us a Sign! (Contact Us!)
Word PDF Epub Text
XML XPS

A leading colon in a [gs batch] [gs file] is ignored because it denotes a label; it is ignored in VBscript because it is a command separator. Thus the batch [gs script] can search itself for lines beginning with a colon and write them out to a temporary vbs file and then execute it.

[tweet]

Example

@echo off

echo This is batch
:wscript.echo "This VBScript"
:wscript.echo "Today is " & day(date) & "-" & month(date) & "-" & year(date)
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs
echo This is batch again

The result

This is batch

This VBScript
Today is 21-4-2010
This is batch again

Maybe you want to use some GUI feature of wscript e.g. to get user input...

Another Example

@echo off

:wscript.echo InputBox("Enter your name")
findstr "^:" "%~sf0">temp.vbs & for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set name=%%N & del temp.vbs
echo You entered %name%

SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “How to Embed VBScript in cmd/bat script (VBscript vs. Batch DOS)”

Comments are closed.