Computing.Net > Forums > Programming > Incrementing a variable name batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Incrementing a variable name batch

Reply to Message Icon

Name: pball
Date: October 5, 2008 at 12:52:48 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

I'm working on a script (same one that's been in my last 4-5 posts) and I want to read some settings from a separate txt file. I'm surprised I was easily able to make the script work except for one part. I'm guessing there is some trick or something to get this to work.

@echo off
setlocal EnableDelayedExpansion

set num=1

for /f "tokens=*" %%a in (settings.txt) do (
set !num!=%%a
)
pause

The first line in the loop doesn't set the value of !num! to the value of %%a. I'm guessing you can't have a variable set to another variable.

How can I have an incrementing number set equal to the contents of the line that is being read.

So when the script is done, each line in the settings file would be a variable that is the line number it comes from in the file.

edit
found a simpler way

for /f "tokens=*" %%a in (settings.txt) do (
set %%a
)

with the setting file set up like this

hey=c:\watever\hey.exe

works, is simple, makes the script smaller. I love getting scripts as small and efficient as possible.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 5, 2008 at 15:18:11 Pacific
Reply:

It is possible to set the string inside a variable to another value eg set %var%=whatever. You, however, can not use variables that start with numbers in a batch file(it can be done on the command line). For example the variable %a9% is valid but the variable %9a% is not. This is because they conflict with command line agruments eg %0, which of course is the name of the batch file.


0

Response Number 2
Name: pball
Date: October 5, 2008 at 15:44:23 Pacific
Reply:

Thanks I did not know that. I'm glad I still posted this cause I learned something very good to know.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Incrementing a variable name batch

Search a file name, Batch www.computing.net/answers/programming/search-a-file-name-batch/17080.html

Set a variable to the running bat www.computing.net/answers/programming/set-a-variable-to-the-running-bat/16395.html

Batch File- Subfolder as a Variable www.computing.net/answers/programming/batch-file-subfolder-as-a-variable/14153.html