Solved

How to accept Input in Batch file

Posted on 2002-05-07
7
1,645 Views
Last Modified: 2012-06-21
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.

0
Comment
Question by:ashok73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 6996130
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
 

Author Comment

by:ashok73
ID: 6996138
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
 
LVL 11

Expert Comment

by:dimitry
ID: 6996148
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 7

Accepted Solution

by:
rin1010 earned 100 total points
ID: 6997100

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
 

Author Comment

by:ashok73
ID: 7001078
rin1010 Thanks for your help.
thanks to Dimiltry as well..

Bye.


0
 

Author Comment

by:ashok73
ID: 7001080
rin1010 Thanks for your help.
thanks to Dimiltry as well..

Bye.


0
 
LVL 7

Expert Comment

by:rin1010
ID: 7002112

You're most welcome, ashok...
btw... Was the B grade the default
or are my comments somehow deficient?
 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
robocopy for today files 14 268
Need to Copy files and folders in a batch file 2 94
Build VS2015 project in bat command 3 108
Set variable with 'OR' logic possible? 12 17
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question