You can run a VBScript from a batch file like this (the parameter //NoLogo
prevents the interpreter from printing the version/copyright banner all the time):
cscript //NoLogo C:\your.vbs 23
Use the Arguments
property for receiving the argument (23
) in the VBScript:
value = WScript.Arguments(0)
and pass a value back to the batch script via the return value:
WScript.Quit 42
Example:
VBScript code:
value = WScript.Arguments(0)
WScript.Quit value + 19
Batch code:
@echo off
cscript //NoLogo C:\your.vbs %1
echo %errorlevel%
Output:
C:\>batch.cmd 23
42
C:\>batch.cmd 4
23
If you need to pass text back and forth, passing the response back to the batch script becomes more complicated. You'll need something like this:
for /f "tokens=*" %%r in ('cscript //NoLogo C:\your.vbs %1') do set "res=%%r"
Example:
VBScript code:
str = WScript.Arguments(0)
WScript.StdOut.WriteLine str & " too"
Batch code:
@echo off
setlocal
for /f "tokens=*" %%r in ('cscript //NoLogo C:\your.vbs %1') do set "res=%%r"
echo %res%
Output:
C:\>batch.cmd "foo"
foo too
C:\>batch.cmd "and some"
and some too
On a more general note: why do you use batch/VBScript in the first place? PowerShell is far more versatile than both of them combined, and is available for any halfway recent Windows version.