Not the right command with the right option (errorlevel)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Not the right command with the right option (errorlevel)

#1 Post by RCP » 15 Sep 2013 15:52

Hi people,

I just created a batch file under FreeDOS to automate a Ghost image creation.
But it has a strange behaviour: If you press a number, the errorlevel does not go to the right number.
So if I type nr. 1 it should make a ghost image on HD (:1) but instead it starts ghost (:7)

echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo º ***MAINMENU*** º
echo ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo º 1. BACKUP ON HD º
echo º 2. BACKUP HD RESTORE º
echo º 3. BACKUP HD FILE CHECK º
echo º 4. BACKUP ON DVD º
echo º 5. BACKUP FROM DVD º
echo º 6. BACKUP DVD FILE CHECK º
echo º 7. START GHOST º
echo º 8. REBOOT º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ

choice /C:12345678 /N Choose a option...
if errorlevel 8 goto exit
if errorlevel 7 goto 7
if errorlevel 6 goto 6
if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
if errorlevel 1 goto 1

:7
echo Start ghost...
pause
Ghost.exe
goto start

:6
pause
SET TZ=GHO-01:00
echo BACKUP DVD FILE CHECK...
pause
echo
ghost.exe -CHKIMG,@CD-R1\windowsbackup.GHO
goto start

:5
SET TZ=GHO-01:00
echo BACKUP FROM DVD...
pause
ghost.exe -clone,mode=pload,src=@CD-R1:\windowsbackup.GHO:[1],dst=[2:4]
GOTO start

:4
SET TZ=GHO-01:00
echo BACKUP ON DVD...
pause
ghost.exe -clone,mode=pdump,src=[2:4],dst=@CD-R1\windowsbackup.gho -Z9 -auto
goto start

:3
SET TZ=GHO-01:00
echo BACKUP HD FILE CHECK ...
pause
ghost.exe -CHKIMG,[2:5]\windowsbackup.GHO -SURE
goto start

:2
SET TZ=GHO-01:00
echo BACKUP HD RESTORE ...
pause
ghost.exe -clone,mode=pload,src=[3.6]:\windowsbackup.GHO:[1],dst=[3:2]
GOTO start

:1
SET TZ=GHO-01:00
echo BACKUP ON HD...
pause
goto start

As you can see after pressing 1 it should go to 1 but instead it executes 7.
Even if I put errorlevel 1 all up it does not work.
Also not working is change the goto 1 to goto 7 for errorlevel 1 and cheange the :1 into :7.
If I type option 2, it should go 2 :2 but instead it goes to :6

I really have no clue what's going on here.
Searched at google but no really good answer.

Hope someone can help me out :cry:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Not the right command with the right option (errorlevel)

#2 Post by foxidrive » 15 Sep 2013 21:20

Add the pause and goto :eof then try it. There may be a different issue.


RCP wrote:

Code: Select all

echo          ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo          º         ***MAINMENU***          º
echo          ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo          º  1. BACKUP ON HD          º
echo          º  2. BACKUP HD RESTORE     º
echo          º  3. BACKUP HD FILE CHECK     º
echo          º  4. BACKUP ON DVD         º
echo          º  5. BACKUP FROM DVD º
echo          º  6. BACKUP DVD FILE CHECK   º
echo          º  7. START GHOST               º
echo          º  8. REBOOT          º
echo          ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ

choice /C:12345678 /N Choose a option...
if errorlevel 8 goto exit
if errorlevel 7 goto 7
if errorlevel 6 goto 6
if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
if errorlevel 1 goto 1

pause
goto :eof

:7
echo Start ghost...
pause
Ghost.exe
goto start


bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: Not the right command with the right option (errorlevel)

#3 Post by bars143 » 15 Sep 2013 22:53

im newbie here from philippine and not a programmer but a learner to batch script and this thread regarding above code that i test is really working with the use of "&echo" as shown below:

Code: Select all

choice /C:12345678 &echo Choose a option...



this code below does not work (in my window7 32bit using dell netbook):

Code: Select all

choice /C:12345678 /N Choose a option...



finally to fellow members --i introduced myself as a new learner with no programming background -- and eager to learn on this forum as i love to have a netbook as my basis of study. thank.

anyway above reply of my suggestion really work to anyone? :?:

edited twice due to issue of using bold tag in the code tag.
and of "&" is missing :oops:
Last edited by bars143 on 15 Sep 2013 23:01, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Not the right command with the right option (errorlevel)

#4 Post by foxidrive » 15 Sep 2013 23:00

Welcome to Dostips, Bars143.

You are quite right - the choice syntax was incorrect. This works.

Code: Select all

choice /C:12345678 /N /M "Choose a option..."

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: Not the right command with the right option (errorlevel)

#5 Post by bars143 » 15 Sep 2013 23:15

foxidrive wrote:Welcome to Dostips, Bars143.

You are quite right - the choice syntax was incorrect. This works.

Code: Select all

choice /C:12345678 /N /M "Choose a option..."


ahh another way foxi -- and its really work.


anyway ,thanks for welcoming me.

i felt happy to have this forum as i planned to fully use my netbook to batch scripting. in my country Philippines -- having computer is a luxury to have it since its an update skill for developing country like Philippines.

and i already help my fellow neighbors to fully utilize part of computer daily use - even though i have no schooling about but just a curiosity( and try and error)

though, i already a guest visitor to this forum for nearly 5-months from now.until today im on unemployment status and learn way to have my time in this forum. :D

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Not the right command with the right option (errorlevel)

#6 Post by Aacini » 16 Sep 2013 00:05

Code: Select all

choice /C:12345678 /N Choose a option...
if errorlevel 8 goto exit
if not errorlevel 1 goto exit

goto %errorlevel%

:7
. . . .

RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Re: Not the right command with the right option (errorlevel)

#7 Post by RCP » 16 Sep 2013 02:40

@bars143: Hi, I respect that you also have a problem and want it, just like me, be solved.
But it is not really properly to post your question in another one's post.
In this way the thread is getting messy with answers/questions from/for me and/or you and it's not gonna read clearly.

Could you please next time open your own post?
It's better for you and the rest of the users on this forum.
Cheers.
Last edited by RCP on 16 Sep 2013 03:53, edited 2 times in total.

RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Re: Not the right command with the right option (errorlevel)

#8 Post by RCP » 16 Sep 2013 02:52

@foxidrive: Thanks for your reply.
Tried your pause and :eof but I get the message batchfile c:\autoexec,bat does not contain label of "eof".
I think I should make such a label but for what?

RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Re: Not the right command with the right option (errorlevel)

#9 Post by RCP » 16 Sep 2013 03:18

Aacini wrote:

Code: Select all

choice /C:12345678 /N Choose a option...
if errorlevel 8 goto exit
if not errorlevel 1 goto exit

goto %errorlevel%

:7
. . . .


I am not very familiar with batch files.
Tried your solution as you can see in the code beneath.
Did I implemented it OK?
Now I get the error: bad command or filename - "rt".

choice /C:12345678 /N Maak je keuze...

if errorlevel 8 goto exit
if not errorlevel 1 goto exit
goto 8

if errorlevel 7 goto 7
if not errorlevel 1 goto exit
goto 7

if errorlevel 6 goto 6
if not errorlevel 1 goto exit
goto 6

if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
if errorlevel 1 goto 1

:8
exit

:7
echo Start ghost...
pause
Ghost.exe
goto end

:6
pause
SET TZ=GHO-01:00
echo Bezig om het Windows backup bestand vanaf DVD te controleren op fouten...
pause
echo
ghost.exe -CHKIMG,@CD-R1\windowsbackup.GHO
goto end

RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Re: Not the right command with the right option (errorlevel)

#10 Post by RCP » 16 Sep 2013 04:01

I also tried aacini's original version.
If I push option 8,7 or 6 it keeps going to label :7

choice /C:12345678 Maak je keuze...

if errorlevel 8 goto exit
if not errorlevel 1 goto exit

goto 8
if errorlevel 7 goto exit
if not errorlevel 1 goto exit

goto 7

if errorlevel 6 goto exit
if not errorlevel 1 goto exit

goto 6


:8
exit

:7
echo Start ghost...
pause
Ghost.exe
goto end

:6
pause
SET TZ=GHO-01:00
echo Bezig om het Windows backup bestand vanaf DVD te controleren op fouten...
pause
echo
ghost.exe -CHKIMG,@CD-R1\windowsbackup.GHO
goto end

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Not the right command with the right option (errorlevel)

#11 Post by foxidrive » 16 Sep 2013 04:27

I realise now that you're not using a cmd window.

Put a pause after the choice line. See if it gives you an error.

RCP
Posts: 12
Joined: 15 Sep 2013 15:10

Re: Not the right command with the right option (errorlevel)

#12 Post by RCP » 16 Sep 2013 04:43

foxidrive wrote:I realise now that you're not using a cmd window.

Put a pause after the choice line. See if it gives you an error.


That's correct :wink:
I use FreeDOS as mentioned in the first post.
I already used a pause function to see what happened.
When you choose option one it goes to option 7.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Not the right command with the right option (errorlevel)

#13 Post by foxidrive » 16 Sep 2013 05:15

Test this - it works here with the FreeDOS choice.exe

This is the same structure as your code - there is something else happening there...

Code: Select all

@echo off 
choice /C:12345678 /N Choose a option...
if errorlevel 8 goto end
if errorlevel 7 goto 7
if errorlevel 6 goto 6
if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
if errorlevel 1 goto 1
GOTO END


:8
ECHO 8
GOTO END
:7
ECHO 7
GOTO END
:6
ECHO 6
GOTO END
:5
ECHO 5
GOTO END
:4
ECHO 4
GOTO END
:3
ECHO 3
GOTO END
:2
ECHO 2
GOTO END
:1
ECHO 1
GOTO END

:END
echo end
PAUSE



Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Not the right command with the right option (errorlevel)

#14 Post by Aacini » 16 Sep 2013 07:16

@RCP:

I used to post just the relevant part of my answers because it is shorter and cleaner, but I post the whole solution below to avoid confusions. This should work as long as FreeDOS replace %errorlevel% by the proper value.

Code: Select all

echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo º ***MAINMENU*** º
echo ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo º 1. BACKUP ON HD º
echo º 2. BACKUP HD RESTORE º
echo º 3. BACKUP HD FILE CHECK º
echo º 4. BACKUP ON DVD º
echo º 5. BACKUP FROM DVD º
echo º 6. BACKUP DVD FILE CHECK º
echo º 7. START GHOST º
echo º 8. REBOOT º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ

choice /C:12345678 /N Choose a option...
if errorlevel 8 goto exit
if not errorlevel 1 goto exit

goto %errorlevel%

:7
echo Start ghost...
pause
Ghost.exe
goto start

:6
pause
SET TZ=GHO-01:00
echo BACKUP DVD FILE CHECK...
pause
echo
ghost.exe -CHKIMG,@CD-R1\windowsbackup.GHO
goto start

:5
SET TZ=GHO-01:00
echo BACKUP FROM DVD...
pause
ghost.exe -clone,mode=pload,src=@CD-R1:\windowsbackup.GHO:[1],dst=[2:4]
GOTO start

:4
SET TZ=GHO-01:00
echo BACKUP ON DVD...
pause
ghost.exe -clone,mode=pdump,src=[2:4],dst=@CD-R1\windowsbackup.gho -Z9 -auto
goto start

:3
SET TZ=GHO-01:00
echo BACKUP HD FILE CHECK ...
pause
ghost.exe -CHKIMG,[2:5]\windowsbackup.GHO -SURE
goto start

:2
SET TZ=GHO-01:00
echo BACKUP HD RESTORE ...
pause
ghost.exe -clone,mode=pload,src=[3.6]:\windowsbackup.GHO:[1],dst=[3:2]
GOTO start

:1
SET TZ=GHO-01:00
echo BACKUP ON HD...
pause
goto start

:exit


Antonio

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

Re: Not the right command with the right option (errorlevel)

#15 Post by aGerman » 16 Sep 2013 12:01

@Antonio
The %errorlevel% variable is Windows NT (cmd.exe) specific and unfortunately not available under DOS.

@RCP
Your code looks right. I'm not familiar with FreeDOS but perhaps you should try to rename the labels to a, b, c ... or whatever. Maybe it doesn't work with numeric names (only a guess though).

Regards
aGerman

Post Reply