From "Frank P. Westlake" :
>A method of launching VBS without first writing an
>external file:
Here's a better routine; the only thing dirty is that
"ECHO OFF" is printed to the console.
:' BEGIN SCRIPT ::::::::::::::::::::::::::::::::::::::
:' demo1.cmd
:' From the desk of Frank P. Westlake, 2012-07-15
:OFF=1:function ECHO(x):end function
ECHO OFF
ECHO 1'>NUL:&CSCRIPT //NOLOGO //E:VBS "%~f0" %*&Goto :EOF
' All VBS from here down.
wscript.echo("This is a VB script.")
' Simulate a very important job.
set a=wscript.arguments
n=a.count-1
if n>=0 then
wscript.echo("Arguments:")
for i=0 to n
wscript.echo("arg "&i&"="&a(i))
next
else wscript.echo "No arguments to print."
end if
' End of very important job simulation.
:' END SCRIPT ::::::::::::::::::::::::::::::::::::::::
There are three lines to add to the top of a VBS script:
:OFF=1:function ECHO(x):end function
ECHO OFF
ECHO 1'>NUL:&CSCRIPT //NOLOGO //E:VBS "%~f0" %*&Goto :EOF
The first line makes ECHO a VBS function that requires
one parameter, and it makes OFF a VBS variable which is
used as the argument for function ECHO.
VBS doesn't require parentheses for the arguement list
so the second line now has valid meaning both for CMD
and VBS.
The third line hides the CSCRIPT command from VBS with
the VBS comment character '; but this line must also be
acceptable to CMD so the dual purpose ECHO is used
again. From the redirection to NUL: to the end of the
line is a comment for VBS and valid instruction for CMD.
This particular demo script does not require that
anything be done in CMD so it could be saved as a VBS
file and called directly with CSCRIPT, but the procedure
allows for pre and post CMD processing all within a
single file. An example:
:' BEGIN SCRIPT ::::::::::::::::::::::::::::::::::::::
:' demo2.cmd
:' From the desk of Frank P. Westlake, 2012-07-15
:OFF=1:function ECHO(x):end function
ECHO OFF
:' Three lines for CMD;
ECHO 1'>NUL:&Set /P "password=Enter your password: "
ECHO 1'>NUL:&Echo CMD: password=%password%
ECHO 1'>NUL:&CSCRIPT //NOLOGO //E:VBS "%~f0" %password%&Goto :'Post
' All VBS from here to :'Post.
' Set and print the password.
set a=wscript.arguments
if a.count>0 then
password=a(0)
wscript.echo "VBS: password="&password
else wscript.echo "VBS: password given."
end if
:'Post
:' All this for CMD:
ECHO 1'>NUL:&Echo CMD: clearing password...
ECHO 1'>NUL:&Set "password="
ECHO 1'>NUL:&Set "password"
ECHO 1'>NUL:&Goto :EOF
:' END SCRIPT ::::::::::::::::::::::::::::::::::::::::
The Post routine could immediately follow the call to
CSCRIPT and Goto :'Post would not be necessary, but I
wanted to show how to use the label.
I don't have offline documentation for VBS so I can't
figure out how to bypass the '@' invalid character
problem that prevents hiding ECHO OFF.
Frank