Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input a string from user into environment variable from batch file

I want to prompt the user for some input detail, and then use it later as a command line argument.

like image 209
EFraim Avatar asked Oct 04 '09 08:10

EFraim


People also ask

What is %% A in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

Can a batch file prompt for input?

You are able to prompt a user for input using a Batch script function.

What is %~ dp0 in batch script?

The %~dp0 Variable. The %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file.


2 Answers

You can use set with the /p argument:

SET /P variable=[promptString]

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

So, simply use something like

set /p Input=Enter some text:  

Later you can use that variable as argument to a command:

myCommand %Input% 

Be careful though, that if your input might contain spaces it's probably a good idea to quote it:

myCommand "%Input%" 
like image 86
Joey Avatar answered Oct 07 '22 10:10

Joey


A rather roundabout way, just for completeness:

 for /f "delims=" %i in ('type CON') do set inp=%i 

Of course that requires ^Z as a terminator, and so the Johannes answer is better in all practical ways.

like image 20
EFraim Avatar answered Oct 07 '22 09:10

EFraim