Jump to content
Can't remember your login details? Read more... ×
UserInterface

Batch file menu

Recommended Posts

Hey guys anyone remember there DOS roots?

 

I want to create a menu in a bat file to run one of two scripts I created, but I am missing something... Screen just flashes up and disappears with no menu.

 

Heres what i had...

 

 

 

@ECHO OFF
cls
ECHO "To create new user please select one of the following groups. "
@ECHO OFF
ECHO 1 - Contracting
ECHO 2 - Property
ECHO 3 - Quit Menu
@ECHO OFF
CHOICE /C:123 /N /T:2,5 Please choose a menu option
@ECHO OFF
IF ERRORLEVEL == 3 GOTO QUIT
IF ERRORLEVEL == 2 GOTO PROPERTY
IF ERRORLEVEL == 1 GOTO CONTRACTING
@ECHO OFF
:CONTRACTING
cls
f:
CSCRIPT NewUserContracting.vbs
exit
@ECHO OFF
:PROPERTY
cls
f:
CSCRIPT NewUserProperty.vbs
exit
@ECHO OFF
:QUIT
exit

Share this post


Link to post
Share on other sites

Jeez dude...that is some serious BAT creation. I've tried some .bat programs very similar and never got positive results no matter what I did. Just create two batch files for your "contracting" and "property" cscripts separately with names "1.bat" and "2.bat" then a simple echo .bat file for noobs using your system in DOS (or I imagine DosBOX emulator).

@ECHO OFF
CLS
ECHO.
ECHO.
ECHO "To create a new user, type the corresponding number and press ENTER key."
ECHO.
ECHO 1 -- Contracting
ECHO 2 -- Property
ECHO.
ECHO.

I don't think this'll work, but your "IF" usage requires a "THEN" before "GOTO" and an "ELSE" before each subsequent "IF." Then a title for your files's beginning along with an "ELSE GOTO %TITLE." I believe I tried that exact same format before even with the loop routine and all I got was either the same as your results or an endless loop I had to ^C out of.

@ECHO OFF
:HOME
cls
ECHO "To create new user please select one of the following groups. "
@ECHO OFF
ECHO 1 - Contracting
ECHO 2 - Property
ECHO 3 - Quit Menu
@ECHO OFF
CHOICE /C:123 /N /T:2,5 Please choose a menu option
@ECHO OFF
IF ERRORLEVEL == 3 THEN GOTO QUIT
ELSE
IF ERRORLEVEL == 2 THEN GOTO PROPERTY
ELSE
IF ERRORLEVEL == 1 THEN GOTO CONTRACTING
ELSE
GOTO HOME

:CONTRACTING
cls
f:
CSCRIPT NewUserContracting.vbs
GOTO :QUIT

:PROPERTY
cls
f:
CSCRIPT NewUserProperty.vbs
GOTO :QUIT

:QUIT
exit
This might work, but since it's a .bat file that runs a subsequent program and DOS isn't too friendly with batches continuing once other programs have been called, your batch will likely terminate itself once CSCRIPT executes, IF it executes at all... Edited by wlayton27

Share this post


Link to post
Share on other sites

LOL, i think i am closer if i do this.

but the just goes stright to running :CONTRACTING (No menu)

 

@ECHO OFF
cls
ECHO "To create new user please select one of the following groups. "
@ECHO OFF
ECHO 1. - Contracting
ECHO 2. - Property
ECHO 3. - Quit Menu
@ECHO OFF
CHOICE /C:123 /N /T:2,5 Please choose a menu option
@ECHO OFF
IF ERRORLEVEL == 3 THEN GOTO QUIT
IF ERRORLEVEL == 2 THEN GOTO PROPERTY
IF ERRORLEVEL == 1 THEN GOTO CONTRACTING
@ECHO OFF
:CONTRACTING
cls
f:
call CSCRIPT NewUserContracting.vbs
exit
@ECHO OFF
:PROPERTY
cls
f:
call CSCRIPT NewUserProperty.vbs
exit
@ECHO OFF
:QUIT
exit

Share this post


Link to post
Share on other sites

how did DOS get this hard again!!

I can do this with VBS, but it took me months to get these scripts working, creating a menu would be out of my skill i think

Share this post


Link to post
Share on other sites

how did DOS get this hard again!!

I can do this with VBS, but it took me months to get these scripts working, creating a menu would be out of my skill i think

Don't fret too much about the menu. You COULD (if you're really putting apps on a full MSDOS installation) use QBasic to create the menu and you'll be helped during creation on syntax and such. I personally loved QBasic so much, I'm emulating it in Win Vista today. Or you could try FoxPRO (Which better be free by now) which my father used to create a full business accounting package for just one company and never disto'd it.

 

btw: you forgot to add "ELSE" to your "IF" statements. MSDOS 5.0 manual says all batch file IF statements need both the THEN and ELSE statements to follow in order for the batch reader to understand its syntax... A syntax error is causing your batch file to run the first command it sees; the command caused the .bat file to terminate itself on its "call."

 

...and this loop command concerns me. I think even if you eventually get the batch file working the program will have to clear screen and refresh several dozen times per second creating a nasty flicker effect and the text may not even be readable. --without the loop to ":home" and ":home" before "cls" as I recommended in my code your screen will scroll the menu's echo commands in a loop causing an unreadable moving bunch of jargon.

 

...another point is this "CHOICE" command. Never used it myself. Always used %1 %2 and so forth after the command, but these variables are extensions that must be following the batch file's name in the first place...

 

...are you using this as an "autoexec" in an emulation, or is this a program that will be double-clicked inside Windows?

 

*edit*

I'm off to bed 'cause it's late here in the US. I'll hafta answer my own questions. I'm assuming you're in WinXP or Vista and you're creating a batch file that will become linked via an icon on the desktop. The newest versions of Windows use a "command" protocol for their prompt setup and MSDOS batch files are completely obsolete. You'd have better luck emulating DOS with a mounted DosBOX partition ... installing full MSDOS5.0 downloads (free) to the mounted directory tree ... then you just add the code for your batch file in the DOSBOX.CFG file to create an AUTOEXEC.BAT inside the partition.

 

...just notice you added the "call" command before each "CSCRIPT" command; if CSCRIPT is a batch file, that batch file needs to include " %1" after its primary command, and calling the second .bat file will terminate the first (so you can nix all "exit" commands and forward them to the next batch files). If CSCRIPT is a .com or .exe file, you need to get rid of your "call" command and run the program normally from inside the .bat file. "Test drive" your batch files by replacing the called commands with "echo" commands and you'll be troubleshooting your syntax in no time.

 

*edit no. 2*

...awww hell. I'll just give you the .bat file you should be starting with::

@ECHO OFF
:HOME
cls
ECHO To create new user please select one of the following groups.
ECHO.
ECHO 1 - Contracting
ECHO 2 - Property
ECHO 3 - Quit Menu
ECHO.
CHOICE /C:123 /N /T:2,5 Please choose a menu option;This command shows the "Please choose a menu option>" prompt
;and awaits user input for numbers "1" "2" or "3"
ECHO.
IF ERRORLEVEL == 3 THEN GOTO QUIT
ELSE
IF ERRORLEVEL == 2 THEN GOTO PROPERTY
ELSE
IF ERRORLEVEL == 1 THEN GOTO CONTRACTING
ELSE
GOTO HOME

:CONTRACTING;This results from user selecting no. one in the main menu
ECHO Command is "cls"
ECHO Command is "f:"
ECHO Command is "CSCRIPT NewUserContracting.vbs"
GOTO :QUIT

:PROPERTY;This results from user selecting no. two in the main menu
ECHO Command is "cls"
ECHO Command is "f:"
ECHO Command is "CSCRIPT NewUserProperty.vbs"
GOTO :QUIT

:QUIT;This results from operations completing their cycle OR user selecting no. three in the main menu
exit
Edited by wlayton27

Share this post


Link to post
Share on other sites

btw: you forgot to add "ELSE" to your "IF" statements. MSDOS 5.0 manual says all batch file IF statements need both the THEN and ELSE statements to follow in order for the batch reader to understand its syntax... A syntax error is causing your batch file to run the first command it sees; the command caused the .bat file to terminate itself on its "call."

This is compounded by the fact that the IF command returns its own ERRORLEVEL.

 

So another way to do it would have been...

 

CHOICE
SET EL=%ERRORLEVEL%
IF %EL%==1 THEN ....
IF %EL%==2 THEN ....

Also, CSCRIPT is an executable for running VBS files from the command prompt, and therefore needs no CALL. The batch script will resume after CSCRIPT has successfully launched. If you want to wait until CSCRIPT has completed, use "START /WAIT CSCRIPT script.vbs"

 

And I think you got the usage of CALL backwards.

 

CALLing a "sub" batch file (B) from within a "main" batch file (A) will return to batch A once batch B has finished executing, unless B contains "EXIT". Omitting CALL would result in being returned to the command prompt (or the window closing) when batch B has completed, with no subesquent return to batch A.

 

Edit: For ease of debugging, take out the "@ECHO OFF" until things are working properly.

Edited by SquallStrife

Share this post


Link to post
Share on other sites

Thanks guys for all your help, I have never gained this much info from one post!

 

Although I found another soulution, I will revisit this next week to get a grip on it.

I just did not have enough time now.

 

But you guys were a huge help (And quick as always)

 

I ended up finding this way, and got my head around it quicker.

@ECHO off
cls
:start
ECHO.
ECHO 1. Create new "Property" user
ECHO 2. Create new "Contracting" user
ECHO 3. Exit
set choice=
set /p choice=Please select user type: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Contracting
if '%choice%'=='2' goto Property
if '%choice%'=='3' goto Exit
ECHO "%choice%" is not valid please try again
ECHO.
goto start
:Contracting
cscript NewUserContracting.vbs
goto end
:Property
cscript NewUserProperty.vbs
goto end
:Exit
Exit
goto end
:end

This seems to work, but I will test properly from work tomorrow (XP not Vista)

Share this post


Link to post
Share on other sites

Sweet that it works now. Wanted to mention you prolly posted in the wrong spot, but wasn't sure seein' as you were troubleshooting. Tried it out myself without emulation. Can't believe it works in Win XP and Vista!! Here's a few code improvements I tried to smooth things over a bit:

@ECHO off
:start
cls
ECHO.
ECHO Please select a number below and press ENTER
ECHO.
ECHO 1. Create new "Property" user
ECHO 2. Create new "Contracting" user
ECHO 3. Exit
ECHO.
set choice=
set /p choice=Please select user type: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Contracting
if '%choice%'=='2' goto Property
if '%choice%'=='3' goto Exit
ECHO "%choice%" is not valid please try again
ECHO.
pause
goto start
:Contracting
echo "Running Cscript NewUserContracting.vbs"
Cscript NewUserContracting.vbs
pause
goto end
:Property
echo "Running Cscript NewUserProperty.vbs"
Cscript NewUserProperty.vbs
pause
goto end
:Exit
echo "Will exit now..."
echo.
pause
goto end
:end
...moved CLS down so an improper choice results in a fresh screen after the pause so the user sees their mistake... added more pauses to create a bit more of a comfortable environment. The program can be a bit "scary" if it blazes by too fast. This gives users a chance to X out before they screw something up. I didn't test the program with the actual CSCRIPT commands running however, you may need to heed SquallStrife's advice and use "START /WAIT CSCRIPT <filename>" instead of what you have now...for better results.

 

Notice that <SPACEBAR><ENTER>will immediately terminate the program from the menu even with all these pauses...I dunno what that is, but I don't think its an issue...

Share this post


Link to post
Share on other sites

There are some pretty big differences between DOS, DOSBox, and Windows' CMD in the batch language.

 

The COMMAND.COM that gets delivered with Win2K and XP is only there for backwards compatibility with DOS apps, and it's not very good. You should always use CMD as your shell, as this is what batch files will run under when you double-click them, and is Windows' native command processor.

 

Also because COMMAND.COM is deprecated in any 64-bit flavour of Windows.

Share this post


Link to post
Share on other sites

There are some pretty big differences between DOS, DOSBox, and Windows' CMD in the batch language.

 

The COMMAND.COM that gets delivered with Win2K and XP is only there for backwards compatibility with DOS apps, and it's not very good. You should always use CMD as your shell, as this is what batch files will run under when you double-click them, and is Windows' native command processor.

 

Also because COMMAND.COM is deprecated in any 64-bit flavour of Windows.

Yeah I ran in to issues on one IT admins PC as he has moved to VISTA x64 (bastard) so the scriptpw.dll will not register and no work around.. oh well he can just RDP to a win2003 server.

 

Just wanna say thanks again, to everyone you guys have been a great help!

 

If anyone is interested in the script that I made to create new users in AD with term servers and home drive (with permissions ACL) etc, let me know and I will post, there are a few parts that were very hard to work out so might find usefull.

Share this post


Link to post
Share on other sites

If you used labels like :opt_1 :opt_2 :opt_3 you could replace all those:

 

if '%choice%'=='X' goto SOME_LABEL

Instead simply use...

 

for %%g in (1,2,3) do if '%%g'=='%choice%' goto opt_%%g

Given that your running VB scripts, you could even use one label and pass the script names to it.

If would like an example of doing that yell, for now I'll be lazy :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×