I have some problems with nested for-loops. as you see in the result, the correct line was loaded, but the second loop which splits up the line into two fields does somthing wrong
Any ideas ?
cheers endo
current output
line=user1|pass1
usr: user2
pass: pass2
--------
line=user2|pass2
usr: user2
pass: pass2
--------
pass.txt
user1|pass1
user2|pass2
batchfile
cls
@echo off &setlocal
for /f "tokens=*" %%a in (pass.txt) do (
echo line=%%a
set "string=%%a"
for /f "tokens=1,2 delims=|" %%i in ("%string%") do set "variable1=%%i" &set "variable2=%%j"
echo usr: %variable1%
echo pass: %variable2%
endlocal
set "string="
echo. --------
)
Nested for
loops are correctly working.
But what is not working (not as intended) are the variables. In batch script, each time a block (the lines enclosed in parenthesis) is reached, all variable reads are replaced with the value in the variable before the execution. If inside a block you need to access the value of a variable, and this value has been assigned/changed inside the block, then, it is necessary to indicate that the variable read should be delayed.
So, your code should be something like
@echo off & cls
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (pass.txt) do (
echo line=%%a
set "string=%%a"
for /f "tokens=1,2 delims=|" %%i in ("!string!") do set "variable1=%%i" &set "variable2=%%j"
echo usr: !variable1!
echo pass: !variable2!
echo. --------
)
endlocal
setlocal enabledelayexexpansion
activates this feature, and %var%
references are replaced (where needed) with !var!
sintax, indicating variables that need delayed read.