Jump to content

Creating a batch to ask a question


Dw2k

Recommended Posts

Is it possible to make the batch file ask for user input?

reason I ask is that I would like to copy certain files from my D: to my C: during the batch run but i'd like it to ask me if i'd want to or not and not bother coyping them?

Thanks guys, great website you got here! :)

Link to comment
Share on other sites


User input can be done thusly:

@Echo Off
Echo Press 1 if you want to copy files
Echo Press 2 if you do not want to copy files
Echo Please make your selection...

After throwing that code in a batch file (notice it doesnt actually do anything besides display text, but ill get to that in a minute), create 2 more batch files. one named "1.cmd" and the other "2.cmd".

In "1.cmd" you place all the files you want to copy, and in "2.cmd" you pick up the setup where you left off.

Explanation:

In the code above, all it does is print text and displays it to the user. When its done, it brings it back to the command prompt at the directory where the batchfile was run. In this directory is where you place 1.cmd and 2.cmd. When the user presses 1 or 2, it executes the appropriate batch file. Simple!

You can ofcourse change the names of the 1.cmd and 2.cmd, except you'll need to prompt the user to type in the filename of the command file, rather then just 1 or 2.

Hope this helps! :)

Link to comment
Share on other sites

The following batch file snippet displays a simple menu (without a question-mark at the end of the prompt) and prompts for the users choice, defaulting to option 2 after 5 seconds :

@ECHO OFF
ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows. (default)
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
CHOICE /C:1234 /N /T:2,5 Please choose a menu option.
IF ERRORLEVEL == 4 GOTO QUIT_MENU
IF ERRORLEVEL == 3 GOTO DEFRAG_HD
IF ERRORLEVEL == 2 GOTO RUN_WIN
IF ERRORLEVEL == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
:RUN_WIN
CALL WIN
:DEFRAG_HD
DEFRAG c:
:QUIT_MENU

credit: http://www.cs.ntu.edu.au/homepages/bea/hom...escription.html

Link to comment
Share on other sites

Unfortunatly, the Choice command doesnt not work in Windows XP

@ECHO OFF
SET MyInput=
IF NOT '%1'=='' GOTO QUIT_MENU
ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows.
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
SET /P MyInput=Please choose a menu option:
IF %MyInput% == 4 GOTO QUIT_MENU
IF %MyInput% == 3 GOTO DEFRAG_HD
IF %MyInput% == 2 GOTO RUN_WIN
IF %MyInput% == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
:RUN_WIN
CALL WIN
:DEFRAG_HD
DEFRAG c:
:QUIT_MENU

This will work.

Link to comment
Share on other sites

Unfortunatly, the Choice command doesnt not work in Windows XP

@ECHO OFF
SET MyInput=
IF NOT '%1'=='' GOTO QUIT_MENU
ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows.
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
SET /P MyInput=Please choose a menu option:
IF %MyInput% == 4 GOTO QUIT_MENU
IF %MyInput% == 3 GOTO DEFRAG_HD
IF %MyInput% == 2 GOTO RUN_WIN
IF %MyInput% == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
:RUN_WIN
CALL WIN
:DEFRAG_HD
DEFRAG c:
:QUIT_MENU

This will work.

This code works but unfortunatly when you finish using option 1,2 or three it automatically runs on the the next option, this is a revised one which automatically goes back to the menu when finished..

@ECHO OFF
:START
SET MyInput=
IF NOT '%1'=='' GOTO QUIT_MENU
ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows.
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
SET /P MyInput=Please choose a menu option:
IF %MyInput% == 4 GOTO QUIT_MENU
IF %MyInput% == 3 GOTO DEFRAG_HD
IF %MyInput% == 2 GOTO RUN_WIN
IF %MyInput% == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
GOTO START
:RUN_WIN
CALL WIN
GOTO START
:DEFRAG_HD
DEFRAG c:
GOTO START
:QUIT_MENU

Link to comment
Share on other sites

Hey guys, thanks for the quick tutorial on the batch commands. This is very helpful indeed. How would like someone write the code where the user picks up a menu choice and after it is done with that choice, it automatically continues on the installation without going back to the menu? For example, if I wanted to create this menu.

1. Install Windows XP with original default.

2. Install Windows XP (Tweaks Only)

3. Install Windows XP (Tweaks and Themes)

etc..

Also, is there a way to log the output of display in a batch file? The line below the the CALL statement, is that a regular command file (filename.cmd)? I'm to all this stuff.

-Kenneth

Link to comment
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...