Computing.Net > Forums > Programming > Loop That Stores User Input In X Variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Loop That Stores User Input In X Variables

Reply to Message Icon

Name: blazerqb14
Date: August 28, 2009 at 12:52:41 Pacific
OS: Windows Vista Home P
CPU/Ram: Core 2 Quad 8200/6gb
Subcategory: Batch
Comment:

I am trying run a loop X amount of times, and prompt for a string each time and store it in a separate variable for each prompt. Here is what I have:

@ECHO OFF
SETLocal EnableDelayedExpansion

SET /P Number=

:LOOP

set /A Count+=1

IF !Count! gtr !Number! GOTO :eof

SET /P File!Count!=Name of File!Count!?-

Goto LOOP

It loops through and asks me for all the file names, but it doesn't store the values in the variables I want(file1, file2, etc.) I suspect that you can't set a variable name that contains a variable which leads me the real problem: I don't have a clue how to set a "variable" number of variables.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: August 28, 2009 at 13:14:53 Pacific
Reply:

What happens when you try? Are the file variables empty? If so, it's because you're using SETLOCAL and GOTO :EOF


0

Response Number 2
Name: blazerqb14
Date: August 28, 2009 at 14:26:00 Pacific
Reply:

Oh! Yeah, your right. I was changing someone else's loop code for another purpose and I didn't realize what :eof did. So, now I get the variables set correctly.

Which brings me to another thing I don't know how to do; run a command on all those variables. For simplicity, how would I ECHO each "File" variable that is created in the loop into a text file?

@ECHO OFF
SETLocal EnableDelayedExpansion

SET /P Number=

:LOOP

set /A Count+=1

IF !Count! gtr !Number! GOTO :EndLoop

SET /P File!Count!=Name of File!Video!?-

Goto LOOP

:EndLOOP


0

Response Number 3
Name: klint
Date: August 28, 2009 at 15:02:39 Pacific
Reply:

If you are not inside a block (e.g. a FOR loop or anything that
involves surrounding multiple commands in parentheses) then
the following should work:

echo !File%Count%!

(Note that the "%" expansion happens before the "!".)
If you want to iterate over all variables from 1 to Number:

for /l %%i in (1,1,!Number!) do (
   echo !File%%n!
)


0

Response Number 4
Name: blazerqb14
Date: August 28, 2009 at 15:23:21 Pacific
Reply:

Thanks a lot. That's pretty ironic. I tried %File!Count!% instead of !File%Count%! and the /l switch of FOR, but I guess I didn't do it right. Do you have to use %%i and %%n?


0

Response Number 5
Name: klint
Date: September 2, 2009 at 06:36:55 Pacific
Reply:

Sorry, that was a typo. I meant %%i both times, i.e. echo !File%%i!


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Batch File: Integers in a... VB6 code



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Loop That Stores User Input In X Variables

Store the result in a variable www.computing.net/answers/programming/store-the-result-in-a-variable/11122.html

IR i need some help :) www.computing.net/answers/programming/ir-i-need-some-help-/10980.html

remember user inputs www.computing.net/answers/programming/remember-user-inputs/17658.html