Batch files and vbScripts
Wednesday, November 22, 2006 14:35Recently a friend needed to pass a value from a vbscript to a batch file which called the vbscript.
I’ve never had to do this before so after a little bit of head scratching, googling and few rounds of trial/error we managed to solve the problem.
In the process we came across three ways to get a return value of the vbscript into the batch file.
1. Using the “Wscript.Quit” and “%errorlevel%” environment variable
In our script we could use the Quit method to return a integer value when exiting and read that value in our batch file using the %errorlevel% environment variable of the command shell.
Script file myScript.vbs:
‘Do something.
‘
‘Exit with a purpose!
Wscript.Quit(65)
Batch file myBatch.bat:
REM Run the script.
myScript.vbs
REM Do something with the return value.
echo %errorlevel%
This may not look to be interesting at first, but it is possible to get a little creative and use it to pass a drive letter from a script to the command shell.
2. Using a third file to store the result
This somehow seemed uninteresting. But it could do the trick. First we write the our return value from the script to a file, and then in the batch file we read the contents of the file.
Script file myScript.vbs:
Dim oFilesys, oFiletxt, sFilename, returnValue
‘Do something, and return something
returnValue=”cool Result!“
‘Write a return value to a file
Set oFilesys = CreateObject(”Scripting.FileSystemObject”)
Set oFiletxt = oFilesys.CreateTextFile(”returnValue.txt”, True)
oFiletxt.WriteLine(returnValue)
oFiletxt.Close
‘Exit
WScript.Quit(0)
Batch file myBatch.bat:
REM Run the script
myScript.vbs
REM Read the return value
FOR /F %%i in (returnValue.txt) do echo %%i
3. Using “WScript.Echo” with “FOR /F”
Here is what we ended up with, instead of writing anything to a fille, we can just use WScript.Echo to tell the script to spit out the result and then use the For command to read it. Nice and easy!
Script file myScript.vbs:
‘Do something, and return something
WScript.Echo “cool Result!”
‘Exit
WScript.Quit(0)
Batch file myBatch.bat:
REM Run the script and read the return value
For /F “delims=#” %%i in (’cscript //nologo c:\myScript.vbs’) do echo %%i
Here the For will execute the ‘cscript //nologo c:\myScript.vbs’ and read the output. Make sure to add the //nologo unless you want to banner from cscript to also appear as the result. Also if the result contains spaces or tabs, you will need to set the “delims=” because the default delimiter set is space and tab. If we had omitted this in the above example, the retVal would have only contained “cool”.
Any other interesting ways of doing this?
brian H says:
April 2nd, 2007 at 1:34 am
Hi,
I have tried your third and final approach but get an error as follows:
%retVal was unexpected at this time.
Don’t see where I am going wrong, any advice?
Thanks,
Brian H.
Kavinda says:
April 2nd, 2007 at 4:11 am
Hi Brain,
I’ve actually made a typo on the third method, i had used %%retVal as the variable where it should have been just %%i (a single charactor variable).
So, this:
For /F "delims=#" %%retVal in (’cscript //nologo c:\myScript.vbs’) do echo %%retVal
should actually be:
For /F "delims=#" %%i in (’cscript //nologo c:\myScript.vbs’) do echo %%i
Why it should be a single charactor, i dont know. But this is what the help has on it:
C:\Users\Kavinda>for /?
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
brian H says:
April 3rd, 2007 at 9:32 pm
Thanks Kavinda, I did eventually figure it out but it took a while. Works great though, thanks for posting this!
Regards,
Brian H