English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In a batch file, how do i use user input?

how do i use user input using %1, %2 etc from the running bat file, not from the actual file itself, and how do i loop the batch file a set number of times?

How do i make it show something like:

How many times would you like to run this command?:

then it loops the number of times told.

Thanks,

Nappymonster

2007-09-19 07:06:49 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

For example, what would be the code to write something along the lines of:

What is your name? (you enter your name)

How many times do you want to repeat it?

(enter a number)

Hello bob
hello bob
hello bob
etc

2007-09-19 07:14:26 · update #1

4 answers

I would do it this way.

First, use the "set /p" command to ask your questions and populate two environment variables with the answers - the person's name and the number of times to repeat the Hello.

Then, have a loop section that Echo's the Hello phrase and then decrement the variable for looping. When the looping variable becomes zero, then stop echo'ing.

Other Notes,
To change the value of a numeric environment variable, you need to use the "set /a" command.

Like this,


@echo off
set /p zname=What is your name?
set /p zloop=How many times do you want to repeat?
:loop
if "%zloop%"=="0" goto :EOF
echo Hello %zname%
set /a zloop=%zloop%-1
goto :LOOP

2007-09-22 02:01:57 · answer #1 · answered by Kevin 7 · 4 0

That isn't quite the way the %n commands work. The idea is that when you enter the name of the batch file you follow it with a parameter or parameters. The %1 is then replaced by the parameter. So if your batch file was called BOB and it contained a WRITE %1 and you entered BOB HELLO then HELLO would be displayed. You are talking about repetitive looping and soliciting input which normally is done with some kind of program. It might be possible with a batch file but I don't know how.

2007-09-19 14:19:01 · answer #2 · answered by Moondog 7 · 1 1

for i in 1 2 3 4 5
do
echo "Hello Bob"
done

2007-09-19 14:24:24 · answer #3 · answered by Runa 7 · 0 4

Hmmm...might try:

set i=0
:Loop
if i==%2 then exit
set i=i+1
type %1
goto Loop

I don't know if my if/then statement is correct, but that's the idea.

2007-09-19 14:28:17 · answer #4 · answered by Thomas K 4 · 2 4

fedest.com, questions and answers