Computing.Net > Forums > Programming > Batch file help!!!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file help!!!

Reply to Message Icon

Name: sirlagalot
Date: May 2, 2009 at 16:35:09 Pacific
OS: MSDOS
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: May 2, 2009 at 18:39:09 Pacific
Reply:

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.


0

Response Number 2
Name: sirlagalot
Date: May 3, 2009 at 13:43:24 Pacific
Reply:

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.bat

also, 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.


0

Response Number 3
Name: Judago
Date: May 3, 2009 at 17:08:27 Pacific
Reply:

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.bat

To 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.bat

It 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]


0

Response Number 4
Name: sirlagalot
Date: May 3, 2009 at 17:42:26 Pacific
Reply:

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?


0

Response Number 5
Name: Judago
Date: May 3, 2009 at 18:12:35 Pacific
Reply:

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]


0

Related Posts

See More



Response Number 6
Name: sirlagalot
Date: May 4, 2009 at 08:48:06 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file help!!!

batch file help www.computing.net/answers/programming/batch-file-help/13830.html

Batch File Help www.computing.net/answers/programming/batch-file-help-/11464.html

batch file help www.computing.net/answers/programming/batch-file-help/190.html