Solved How Do i load batch variables from a file

September 13, 2012 at 23:12:55
Specs: Windows Vista
Hey guys i was wondering how to load a variable from a file, anyone know?
thanks for any feedback

Domo!


See More: How Do i load batch variables from a file

Report •


#1
September 13, 2012 at 23:33:56
sorry what i meant was how do i load more than 1 line out of a file

Domo!


Report •

#2
September 14, 2012 at 01:41:40
✔ Best Answer
:: ===== script starts here ===============
::
:: chuey.bat 2012-09-14 15:37:53.32
@echo off & setLocal enableDELAYedeXpansioN

set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set/a N+=1
set V!N!=%%a
)
set V
::====== script ends here =================


====================================
Life is too important to be taken seriously.

M2


Report •

#3
September 14, 2012 at 18:50:33
wait sorry i don't understand does this let me load more than 1 variable from the file?
e.g.
these are my variables
set a=
set b=
set c=
how do i set out the file so i can load the "A" variable into the right place, the "b" variable into the right place and the "C" variable into the right place??
sorry I'm not to good with batch -.-
also how do you make it so that once it's loaded the variables it shows them on the screen?

Domo!


Report •

Related Solutions

#4
September 14, 2012 at 20:18:14
Yes, the entire file will be loaded into variables. Line one will be in variable %V1%, line two will be in var. %V2%, etc. because value of var. N goes up +1 for each line in the file, and var name %V!N!% thus progresses from V1, to V2, to V3... It takes getting used to, and familiarity with "delayed expansion" concept.(see: set /?). So, f/e, if !N! is currently 3, and I say: set V!N!=%%a, then variable V3 is then assigned the content of the line from the file. M2 showed how to display "quick" method: set V
or:
for /L %%a in (1 1 %N%) do echo !V%%a!

Report •

#5
September 14, 2012 at 22:27:49
Thanks Guys it really helped!!, but i've hit another problem
my variables are

set Health=
set Armor=
and so on
will i have to change these variables to
set v1=
set v2=
and so on?
(also yes im trying to make a simple batch game -.-)
anyway thanks for previous replies and this will be my last question (for now ( T_T) )

Domo!


Report •

#6
September 15, 2012 at 01:07:37
Post a few lines of your file.


====================================
Life is too important to be taken seriously.

M2


Report •

#7
September 16, 2012 at 17:48:27
-.-
ermm
ok
::batch variables
set health
set armor
set thirst
set hunger

::file that im loading variables from
100
2
100
100
is that what you mean>?

Domo!


Report •

#8
September 17, 2012 at 18:10:07
when done loading, if you want "meaningful" var. names instead of V1 etc, after loading:
set health=%V1%
set armor=%V2%
set hunger=%V3%
set thirst=%V4%

there are other ways to do it, but for a small no. of vars, this might suffice.


Report •

#9
September 17, 2012 at 20:20:39
oh ok thanks i didnt even think of that lol ty :)

Domo!


Report •


Ask Question