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.

ashok73Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rin1010Connect With a Mentor Commented:

ashok,

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 Choice.com 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
cls
echo.
set dirvar=
if "%1"=="" goto usage
if not exist %1\nul goto nodir
set dirvar=%1
dir %1
goto end
:nodir
echo.
echo     Invalid Directory or Path
:usage
echo.
echo     Enter Source Directory
echo.
echo     Example:   %0  C:\Windows
:end
echo.
if not "%dirvar%"=="" echo     You entered:  %DirVar%
echo.


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

...is 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.
 
0
 
dimitryCommented:
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
0
 
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 ???
0
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:
Choice.com 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)
 IF ERRORLEVEL 2 GOTO noAnswer
 ... (yes answer)
 GOTO end
:noAnswer
 ... (no answer)
:end
0
 
ashok73Author Commented:
rin1010 Thanks for your help.
thanks to Dimiltry as well..

Bye.


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

Bye.


0
 
rin1010Commented:

You're most welcome, ashok...
btw... Was the B grade the default
or are my comments somehow deficient?
 
0
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.