You are not logged in.

#1 23 Feb 2012 11:12

Jan
Member
Registered: 21 Feb 2012
Posts: 1

Curious behavior of the variable %ERRORLEVEL%

Hi at all,
is my first post on this list, so sorry for errors and my bad English.
I have a little vbs script that counts the line of a file. The result is returned as ERRERLEVEL.
But the behavior change as follow:

1. If I call direct the script all run fine
2. If I call the script in a "long" loop for, the variable %ERRORLEVEL% is always 0
3. If I call the script in a "short" loop for,  the variable %ERRORLEVEL% is correct.

Follow the code and the result.
Someone could tell me why this behavior and how can I avoid it?
Thank a lot.

###########################
# Code LineCount.vbs:

' Script count the line of FILE and return the result in %ERRORLEVEL%

Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Dim StdIn, StdOut, sLine, iNumLine, sysFile, objFile

if (WScript.Arguments.Count < 1) then
   Sintassi()
   WScript.Quit(-1)
end if

sLine = ""
iNumLine = 0

Set sysFile = CreateObject("Scripting.FileSystemObject")
Set objFile = sysFile.OpenTextFile(WScript.Arguments.Item(0), ForReading) 

Do While Not objFile.AtEndOfStream
    sLine = objFile.ReadLine
    iNumLine = iNumLine + 1
Loop
objFile.Close
WScript.Quit(iNumLine)

Sub Usage()
   WScript.Echo "Usage:"
   WScript.Echo "cscript //NOLOGO LineCount.vbs <File>"
   WScript.Echo 
   WScript.Echo "Wher:"
   WScript.Echo "  File = file to be count" 
   WScript.Echo 
End Sub


###########################
# First test:

# Code test.cmd:

Echo "Ora richiamo vbd direttamente:"
cscript //NOLOGO LineCount.vbs test.cmd
echo %ERRORLEVEL%

# Result:

C:\Test>test.cmd

C:\Test>Echo "Direct call vbs:"
"Direct call vbs:"

C:\Test>cscript //NOLOGO LineCount.vbs test.cmd

C:\Test>echo 3
3

C:\Test>

###########################
# Second Test:

#Code test.cmd:

Echo "Long loop:"

for /F %%i in (test.cmd) do (
    echo "Ciclo: %%i"
    cscript //NOLOGO LineCount.vbs test.cmd
    echo %ERRORLEVEL%
)

echo End script

# Result:

C:\Test>test.cmd

C:\Test>Echo "Long loop:"
"Long loop:"

C:\Test>for /F %i in (test.cmd) do (
echo "Ciclo: %i"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)

C:\Test>(
echo "Ciclo: Echo"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: Echo"
0

C:\Test>(
echo "Ciclo: for"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: for"
0

C:\Test>(
echo "Ciclo: echo"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: echo"
0

C:\Test>(
echo "Ciclo: cscript"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: cscript"
0

C:\Test>(
echo "Ciclo: echo"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: echo"
0

C:\Test>(
echo "Ciclo: )"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: )"
0

C:\Test>(
echo "Ciclo: echo"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 0
)
"Ciclo: echo"
0

C:\Test>echo End script
End script

C:\Test>

###########################
# Thirt test:

# Code test.cmd:

Echo "Short loop:"

for %%i in (1 2) do (
    echo "Ciclo: %%i"
    cscript //NOLOGO LineCount.vbs test.cmd
    echo %ERRORLEVEL%
)

echo End script

# Result:

C:\Test>test.cmd

C:\Test>Echo "Short loop:"
"Short loop:"

C:\Test>for %i in (1 2) do (
echo "Ciclo: %i"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 9
)

C:\Test>(
echo "Ciclo: 1"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 9
)
"Ciclo: 1"
9

C:\Test>(
echo "Ciclo: 2"
 cscript //NOLOGO LineCount.vbs test.cmd
 echo 9
)
"Ciclo: 2"
9

C:\Test>echo End script
End script

C:\Test>

Offline

#2 23 Feb 2012 14:30

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Curious behavior of the variable %ERRORLEVEL%

It's due to the way CMD expands variables.

http://ss64.com/nt/delayedexpansion.html

Try this:

Echo "Long loop:"

setlocal enabledelayedexpansion

for /F %%i in (test.cmd) do (
    echo "Ciclo: %%i"
    cscript //NOLOGO LineCount.vbs test.cmd
    echo !ERRORLEVEL!
)

cmd | *sh | ruby | chef

Offline

Board footer

Powered by