Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get vbs return code in batch file

728 views
Skip to first unread message

Arcane

unread,
Jan 16, 2007, 9:40:19 AM1/16/07
to
Hi

I am running a vbs script from inside a batch file, and I need the
return code (or any other way to get some return value returned):

...
d:\test.vbs //B
echo %errorlevel%
...

but even if test.vbs returns 10, %errorlevel% is always 0.
probalby because internally "cscript d:\test.vbs //B" is used, and
cscript returns 0 ?

so how can I get my value from within the vbs script?

Tom Lavedas

unread,
Jan 16, 2007, 10:54:49 AM1/16/07
to
No, I suspect the real problem is that it is actually running under
Wscript.exe by default. Use an explicit call to cscript.exe and it
will work as you expect (assuming you have a "wsh.quit nnn" line that
ends the script).

Tom Lavedas
============
http://members.cox.net/tglbatch/wsh/

Timo Salmi

unread,
Jan 16, 2007, 11:30:36 AM1/16/07
to
Arcane <yo...@arcor.de> wrote:
> I am running a vbs script from inside a batch file, and I need the
> return code (or any other way to get some return value returned):

An extract from
182662 Jan 13 2007 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

With Visual Basic Script one can have
@echo off & setlocal enableextensions
:: Make a temporary folder
if not exist c:\mytemp mkdir c:\mytemp
::
:: Set the errorlevel
echo WScript.Quit ^5>c:\mytemp\tmp$$$.vbs
cscript //nologo c:\mytemp\tmp$$$.vbs
::
for %%e in (0 1 2 3 4 5 6 7 8 9) do (
if %errorlevel% EQU %%e echo The errorlevel is %%e)
::
:: Clean up
for %%f in (c:\mytemp\tmp$$$.vbs) do del %%f
rmdir c:\mytemp
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
The errorlevel is 5

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Todd Vargo

unread,
Jan 16, 2007, 12:27:41 PM1/16/07
to

"Arcane" <yo...@arcor.de> wrote in message
news:1168958417....@l53g2000cwa.googlegroups.com...

Note, the //B is not required to use .vbs from within a batch. It only
suppresses output and prompts from being displayed. The problem has nothing
to do with host selection, its your method of calling it. Typically we
explicitly call the command line host.

cscript /nologo d:\test.vbs

But the wscript.exe host does return an exit code too. To see it you must
tell the batch to wait for the script to finish, otherwise the batch moves
to the next line before the scripting host even loads.

start /wait d:\test.vbs
-or-
start /wait wscript d:\test.vbs

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

b.ped...@get2net.dk

unread,
Jan 18, 2007, 11:56:06 PM1/18/07
to

Hi Todd

I see that you are still writing here :-)

Today I wanted an errorlevel to see if it's a file or folder or
something else.
I got two solutions both not using VBS, but I can't figure out what one
is best.

@echo off& setlocal enableextensions enabledelayedexpansion
call:isFile "%~1"& if errorlevel 1 goto:eof
As you can see, I only want a file. The rest of my program goes here...
...
goto:eof

Solution 1:
:isFile "X"
set a=%~a1& if "!a:~,1!"=="-" (color 07) else color 11
goto:eof

rem from source/based on this:
(set a=%~a1)
if "%a:~,1%" == "-" (
echo file.
) else (
if "%a:~,1%" == "d" (echo folder ^(path^).
) else echo not a file.
)
goto:eof

Solution 2:
:isFile "X"
if exist "%~1\" (color 11) else if exist "%~1" (color 07) else color
11
goto:eof

rem from source/based on this:
if exist "%~1\" (echo folder ^(path^), or empty.) else if exist "%~1"
(
echo file.) else echo not a file.
goto:eof

Benny,
PS. Todd, I was/(is) here:
My gallery no 1: http://hjem.get2net.dk/b_pedersen/gallery/gallery.htm
My gallery no 2:
http://www.photoshopelementsuser.com/gallery/view_img.php?id=11820

btw. Todd, try a click on a url with the midle mouse button (click with
the mouse wheel) in the new IE7...
:-)

my name.p...@gmail.commmm


Todd Vargo wrote:
> "Arcane" <yo...@arcor.de> wrote in message
> news:1168958417....@l53g2000cwa.googlegroups.com...
> > Hi
> >
> > I am running a vbs script from inside a batch file, and I need the
> > return code (or any other way to get some return value returned):
> >

...Snip...

Todd Vargo

unread,
Jan 19, 2007, 2:38:14 AM1/19/07
to

<b.ped...@get2net.dk> wrote in message
news:1169182566.3...@51g2000cwl.googlegroups.com...

>
> Hi Todd
>
> I see that you are still writing here :-)
>
> Today I wanted an errorlevel to see if it's a file or folder or
> something else.
> I got two solutions both not using VBS, but I can't figure out what one
> is best.

Beauty is in the eye of the beholder. ;-)


snip...


> Benny,
> PS. Todd, I was/(is) here:
> My gallery no 1: http://hjem.get2net.dk/b_pedersen/gallery/gallery.htm
> My gallery no 2:
> http://www.photoshopelementsuser.com/gallery/view_img.php?id=11820
>
> btw. Todd, try a click on a url with the midle mouse button (click with
> the mouse wheel) in the new IE7...
> :-)

IE7 does not install on Window 98, just as well, I don't like it.
BTW, nice photos (and your #1 web scripting is interesting too).

0 new messages