How to accept Input in Batch file

I have a batch file in that i want the user to key in the input , Store it in a variable and then do the processing...
For example
C> abc.bat
It should ask "Enter Source Directory"

Thanks in Advance.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

rin1010Connect With a Mentor Commented:


You said, "Store it in a variable..." Do you mean an environment variable
having a value that can be referenced by other process commands,
or a variable 'replaceable parameter' to pass to the batch file?
And will there need to be only one variable?

You can have the batch file prompt for a variable and then test whether one was given
and process it as needed. The external program is typically used for
getting a single character response, but if you need to get a variable string,
such as the name of a directory as in your abc.bat example,
you can also do it with batch commands.

I'll give a batch file example using your case to prompt for a directory
as a replaceable parameter and also storing it as an environment variable.
You may not need to do both but I'll include it to maybe give you some ideas.

This also includes a command to test whether the given directory name exists
and also may not be what you need but just using your directory example.
The script then branches to various labels depending on what is entered.
As the script obtains and processes the variable two separate ways,
the redundant portions can be removed according to your needs.
If you want to test it, save it as a batch file with any name, e.g.;    abc.bat

@echo off
set dirvar=
if "%1"=="" goto usage
if not exist %1\nul goto nodir
set dirvar=%1
dir %1
goto end
echo     Invalid Directory or Path
echo     Enter Source Directory
echo     Example:   %0  C:\Windows
if not "%dirvar%"=="" echo     You entered:  %DirVar%

The replaceable parameter %1 represents the variable string passed to the batch file.
The example tests whether anything was entered and if not, provides a prompt.
If a string was entered, the script checks whether it's a valid directory.
(Again presuming a "source directory" is what you need.)
If the string isn't a valid path the script informs you.

If the string is okay, an environment variable is set to the value of the string.
The variable in the example is named "dirvar" and is first cleared each time
to not retain invalid settings. So this can set an environment variable
or be used to just get an initial string to process.

The %1 parameter is then processed with the Dir command
or can be used however it's needed. Since the %dirvar% variable
is also set to the string, it can be referenced with equal effect.
(When referring to environment variables with a command,
enclose the variable in percent signs.)

The script merely echoes the value of %dirvar% to the screen at the end
after first testing whether the %dirvar% environment variable is set,
but it can be referenced and processed however is necessary.
(The Echo command followed by a period echoes a blank line
to make the prompts and other screen info more legible
but isn't required as a function of the script.)

So running the Dir command on the %1 parameter like this:

Dir %1 the same as running the Dir command on the %dirvar% variable like this:

Dir %dirvar%

Hope some of this is helpful but if it's not what you mean
please provide some more specific details about your batch file
or any required modifications or if you want specific Choice examples
or other prompting or processing methods.
In DOS the simplest way to pass some data to batch file is
to use command line parameters.
For example if you want to pass source directory to the batch file, you need to do:
 abc.bat c:\srcdir
and in batch file you can use %1 as variable with value c:\srcdir
ashok73Author Commented:
Thanks for the comment . But is it possible to have accepting kind of facility in Batch file ????
I heard there is a command called Choice , Is it correct ???
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

dimitryCommented: is DOS utility that "knows" how to return diferent error level according to the answer.
For example:
 choice (you get [Y/N] and you need to push Y or N)
 ... (yes answer)
 GOTO end
 ... (no answer)
ashok73Author Commented:
rin1010 Thanks for your help.
thanks to Dimiltry as well..


ashok73Author Commented:
rin1010 Thanks for your help.
thanks to Dimiltry as well..



You're most welcome, ashok...
btw... Was the B grade the default
or are my comments somehow deficient?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.