Page 1 of 1

Changing errorlevel numbers

Posted: 10 Aug 2010 14:15
by dosgma
okay, I've spent like two hours debugging a script and now I see this...
Image

Code: Select all

D:\WTF> set /p variable="Enter any value : "
Enter any value : 12

D:\WTF> echo %errorlevel%
1

D:\WTF>dir
 Volume in drive D is Data
 Volume Serial Number is 2851-3F83

 Directory of D:\WTF

11-Aug-10  01:02    <DIR>          .
11-Aug-10  01:02    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)   1,847,672,832 bytes free

D:\WTF> echo %errorlevel%
0

D:\WTF> set /p variable="Enter any value : "
Enter any value : 12

D:\WTF> echo %errorlevel%
0

D:\WTF> set /p variable="Enter any value : "
Enter any value : 12

D:\WTF> echo %errorlevel%
0

D:\WTF> set /p variable="Enter any value : "
Enter any value :

D:\WTF> echo %errorlevel%
1

D:\WTF> set /p variable="Enter any value : "
Enter any value : 12

D:\WTF> echo %errorlevel%
1

D:\WTF>


Now am I just sleepy or does this have a logical reason ???

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 14:26
by !k
Try call echo %errorlevel%

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 14:37
by dosgma
that works, but I wanna know why is it happening ?
There's a

Code: Select all

set /p rcname_local="Change Remote computer name to : "
if %errorlevel% neq 0 (
 set /a track+=1
 )
in my code and it won't work consistently because of this.

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 14:50
by !k
Your batch named .BAT
Change extension to .CMD

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 15:21
by alan_b
I am not sure what the problem is for you.

Is it the apparent anomaly between whether %errorlevel% is either 0 or 1 for the same entry of the value 12 ?

If that is the problem,
I find that some DOS commands and user response may determine the %errorlevel%,
but some commands and responses have no effect on %errorlevel%.

I think your image of the DOS screen shows that for some reason you start with %errorlevel% at 1,
and every you enter the value 12 your errorlevel is NOT altered from what it had been.
Then DIR shows you what DIR shows, and because there are no access denied or other errors,
your %errorlevel% is changed to 0 and does not change when you subsequently enter the value 12.
When you enter a null string then "Set /p variable=" throws a hissy fit, and that gives errorlevel of 1,
and the next entry of 12 has no effect on the error level which is therefore stuck at 1.

Alan

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 15:54
by aGerman
Maybe you could work without errorlevel

Code: Select all

set /p rcname_local="Change Remote computer name to : "
if not defined rcname_local (
  set /a track+=1
)


Regards
aGerman

Re: Changing errorlevel numbers

Posted: 10 Aug 2010 16:54
by dosgma
Yeah I guess I shouldn't depend on %errorlevel% for decision making.
Thanks for the explain @alan_b, makes sense.
@aGerman, yeah I used something similar in the end.