Solved

How to accept Input in Batch file

Posted on 2002-05-07
7
1,642 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

763 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