Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all, I'm writing a batch file for a class project and I've been using errorlevels all along. Three days ago I find out errorlevels are not to be used. I've been looking everywhere for an alternative to errorlevels, but have come up short. The project is due next week and I am FREAKING! Can anyone help?
Also, I was wondering if it's possible to run a program from within a batch file and upon exit of the program, return to the batch file?
Thanks for any help!

What about just using variables instead of errorlevels? So long as your still in the same environment (i.e all scrips are called from a main script) and setlocal is not used it should work. You could even use a variable to tell the main script which script to call next, allowing some scripts to run twice.

Ha thanks, I kind of missed the obvious on that. But I was also wondering about starting a program i.e. msav and after exiting the program how to return to the original batch?
would this sequence be correct?
MSAV
msdos.batalso, to exit the batch and give the user a choice of a: or c:, is this correct?
set /p drive=Which drive would you like to go to? [A,C]
if "%drive%"=='a' a:
if "%drive%"=='c' c:This is for a pure dos environment, which I do not have here at home. I'm a little rusty on some of the workings of dos, but hopefully I can find some help? Thanks.

Pure dos has no set /p command, some dos environments will have choice, with would be the better alternative. Failing that or another utility you may be stuck with:
echo type in y or n, when finished press ctrl+z and enter copy con filename.batTo get the answer you would need a y.bat and n.bat to set a a variable and calling filename should invoke them.
As for:
MSAV
msdos.batIt should work, if it doesn't try calling them.
[edit]
There are also some debug script around to generate choice replacements, I doubt the environment your working on won't have choice though.
[/edit]

Oh crap, it doesn't support set /p?? Is there a way I can have a user make a selection from a menu by using a for loop somehow?

As mentioned above the choice command is the obvious alternative, but to use it you would need to use errorlevels - go figure.
You could use a for loop to display the choices but I can see no way of entering user input. Also DOS only supports the standard for loop - for %v in (set) do command - it doesn't support the /f, /l or /d switches.
The method above is not fool proof but it uses only internal commands. it can be expanded to use more that y or n, but each choice must have a corresponding batch file.
@ECHO OFF :loop set lbl= >1.bat echo @set lbl=goto l1 >2.bat echo @set lbl=goto l2 >3.bat echo @set lbl=goto l3 echo 1. Hello echo 2. Goodbye echo 3. What?? echo Type in a number, then ctrl+z and finally hit enter copy con select.bat > nul call select.bat cls %lbl% echo Bad Selection !!! - Try Again. goto loop :l1 echo label 1 echo hello goto end :l2 echo label 2 echo goodbye goto end :l3 echo label 3 echo I said ENTER A NUMBER !!!!!!!!! goto end :end del select.bat del 1.bat del 2.bat del 3.bat set lbl= pause[edit]Better example[/edit]

Thanks for the help! I tried it out and it worked fine, but I also found this other method that uses the date function with fc.
echo Make your selection, then press enter . . .
fc con nul /lb1 /n | date | find "1:" > en#er.bat
echo set value=%%5> enter.bat
call en#er.bat
del en?er.bat > nul
if "%value%"=="a" a.bat
if "%value%"=="b" b.bat
if "%value%"=="c" c.bat
if "%value%"=="d" d.bat
if "%value%"=="e" e.bat
if "%value%"=="f" f.bat
if "%value%"=="g" goto G
set value=I think it's gonna be alright

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |