Hi, not sure why you're doing it this way, but here's some information and examples.
The code you wrote:
Code: Select all
@echo off&setlocal
:: (list.txt = 1234;abcd)
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0list.txt") do set cod%%a=%%b
echo %cod1234% (result correct "abcd")
set a=1234
DOS reads from % to %, as highlighted in the following.
Black=text. Green=escaped text. Blue=first expansion. Red=second expansion.
echo:%cod%%a% (result wrong)
echo:cod%a% (result wrong)
echo:%cod%a%% (result wrong)
echo:%cod%%a%% (result wrong)
If you didn't know, enabledelayedexpansion allows you to use !var! to expand variables at the time of execution, unlike %var% which expands as soon as the interpreter sees it. %var% is expanded when a FOR loop initializes, before it processes, which is why it becomes static and doesn't change. So, aside from not being static, !var! also allows us to combine to process dynamic variables, !var%dynamic%!. However to process a changing dynamic variable in a FOR loop, use CALL %%var!dynamic!%% (%% escapes to text %). This will expand !dynamic! to it's "value", so the interpreter sees %varvalue%
at execution. This bypasses %var%'s early expansion problem, which makes it effectively the same as !varvalue! would be. Anyhow, with organized syntax, we'll do a basic !var%dynamic%!.
Code: Select all
@echo off&setlocal enabledelayedexpansion
:: (list.txt = 1234;abcd)
for /f "usebackq delims=; tokens=1,2" %%a in ("%~dp0list.txt") do set "cod%%a=%%b"
echo:%cod1234% (result correct "abcd")
set "a=1234"
echo:!cod%a%! (result correct "abcd")
pause
You said you wanted to load your file into an array? A DOS array is accomplished with the following.
This will catalog all of each line, including blank lines. Following this, I'll adapt it to your code.
Code: Select all
@echo off&setlocal enabledelayedexpansion
for /f "delims=] tokens=1*" %%x in ('type mytext.txt ^| find /v /n ""') do (
set /a linecount+=1
set "line!linecount!=%%y"
)
:: Display array.
set line
:: To process array...
for /l %%x in (1,1,%linecount%) do (
:: Your code here. The following outputs the array to newtext.txt:
echo:!line%%x!>>newtext.txt
)
Catalog your list.txt into a DOS array. Does not include blank lines, though it could.
Code: Select all
@echo off&setlocal enabledelayedexpansion
for /f "usebackq delims=; tokens=1,2" %%x in ("%~dp0list.txt") do (
set "line%%x=%%y"
)
:: Display array.
set line
:: To process array... use 'set line'. Variables are organized numerically then alphabetically.
for /f "delims== tokens=1*" %%x in ('set line') do (
echo:Variable name: %%x
echo:Has value of: %%y
)