Solved

How to accept Input in Batch file

Posted on 2002-05-07
7
1,634 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now