Pitdroid
Pitdroid

Reputation: 57

Why doesn't a statement print with a for loop variable when it prints fine with another for loop variable?

So, I'm messing around with some code and I came across a weird issue. I have a string in a file called "tests" that is "1:23195068336843541324" I want a for loop to find the 3rd character and based off of that find a certain amount of characters starting with the 7th character. In this case I want to find the 7th and 8th character. So I came up with this nested for loop:

@ECHO off
SETLOCAL EnableDelayedExpansion
FOR /L %%i IN (1,1,5) DO (
    FOR /F %%j IN ('FINDSTR /C:"%%i:" tests.txt') DO (
        SET PH=%%j
        SET mon=!PH:~2,1!
        ECHO !mon!
        SET test%%i=!PH:~6,%mon%!
        ECHO !test1!))

The ECHO statements are purely for debugging so I can see what is set to what.

But everytime it outputs ECHO is OFF because test1 is never set. I recreated it with this loop:

SETLOCAL EnableDelayedExpansion
SET test=123
SET number=2
FOR /L %%i IN (1,1,5) DO (
SET t%%i=!test:~1,%number%!
ECHO !t1!)

and it works fine. If I put a number in for mon in the first loop it also works fine. What am I doing wrong here? Thanks!

Upvotes: 1

Views: 56

Answers (1)

Squashman
Squashman

Reputation: 14290

I think this is what you are attempting to do. I really don't understand why you are doing what you are doing which is half the battle when trying to help someone code something.

@ECHO off
SETLOCAL EnableDelayedExpansion
FOR /L %%j IN (1,1,5) DO (
    FOR /F %%i IN ('FINDSTR /C:"%%j:" tests.txt') DO (
        SET PH=%%i
        SET mon=!PH:~2,1!
        ECHO mon=!mon!
        FOR /F "delims=" %%G IN ("!mon!") do SET test%%j=!PH:~6,%%G!
        ECHO test%%j=!test%%j!
    )
)

Upvotes: 3

Related Questions