Changing errorlevel numbers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosgma
Posts: 3
Joined: 10 Aug 2010 13:50

Changing errorlevel numbers

#1 Post by dosgma » 10 Aug 2010 14:15

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 ???

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Changing errorlevel numbers

#2 Post by !k » 10 Aug 2010 14:26

Try call echo %errorlevel%

dosgma
Posts: 3
Joined: 10 Aug 2010 13:50

Re: Changing errorlevel numbers

#3 Post by dosgma » 10 Aug 2010 14:37

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Changing errorlevel numbers

#4 Post by !k » 10 Aug 2010 14:50

Your batch named .BAT
Change extension to .CMD

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Changing errorlevel numbers

#5 Post by alan_b » 10 Aug 2010 15:21

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

aGerman
Expert
Posts: 4689
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Changing errorlevel numbers

#6 Post by aGerman » 10 Aug 2010 15:54

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

dosgma
Posts: 3
Joined: 10 Aug 2010 13:50

Re: Changing errorlevel numbers

#7 Post by dosgma » 10 Aug 2010 16:54

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.

Post Reply