You are not logged in.

#1 07 May 2015 01:22

apis
Member
Registered: 30 May 2014
Posts: 18

how to set every lines of a file as batch variable?

Here is it:
I have a file named temp.txt. It contains:
some=
someone=
something=
trash=
explode=
heavy=
...etc (+30 more lines. All lines use = at end as delimiter).
I want to put every lines as variables at batch.
The result I want:
var1=some
var2=someone
var3=something
var4=trash
var5=explode
var6=heavy
var7=...
.....etc.
I already try this with for command but failed.
How?

Offline

#2 07 May 2015 01:38

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: how to set every lines of a file as batch variable?

What did your for loop look like? A for loop is exactly what you would want to use, so you were definitely on the right track.

Offline

#3 07 May 2015 02:31

apis
Member
Registered: 30 May 2014
Posts: 18

Re: how to set every lines of a file as batch variable?

Oops, I forget how is that failed for loop. Emm... Something like this:

for /f "delims==" %%a in (temp.txt) do (...)

Offline

#4 07 May 2015 03:05

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: how to set every lines of a file as batch variable?

You're almost there (and for what it's worth, the for loop itself is perfect)!

You're going to want to make a counter variable and use it to create a new variable for each line. Since you're going to be altering a variable's value inside of a code block, you'll also need to enable delayed expansion.

@echo off
setlocal enabledelayedexpansion

set counter=0
for /f "delims==" %%A in (temp.txt) do (
	set var[!counter!]=%%A
	set /a counter+=1
)

set var
pause

set var will display all variables whose names start with the string "var" - I've just included that to prove that it works.

Offline

#5 07 May 2015 03:53

apis
Member
Registered: 30 May 2014
Posts: 18

Re: how to set every lines of a file as batch variable?

It's Working! Thanks. wink

Offline

Board footer

Powered by