Why does this fail, and how to fix it ? :- "%VAR_!A_2!%"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Why does this fail, and how to fix it ? :- "%VAR_!A_2!%"

#1 Post by alan_b » 08 Dec 2011 12:28

I know that this MAY work SOMETIMES :-
"!VAR_%A_2%!"
but only if %A_2% is the same as !A_2!.
e.g.
I successfully run a loop that :-
increments variable "A_2"
and ADDS to an array of VAR_
SET "VAR_!A_2!=!Some_String!".

If I then run a similar loop that :-
increments variable "A_2"
and READS from the array of VAR_
ECHO( "VAR_!A_2!" = "%VAR_!A_2!%"
Instead of first expanding !A_2! into an index number it simply expands an undefined variable,
I am guessing it attempts to expand %VAR_A_2%

Does CMD.EXE defer all !index! type delayed expansion until successful OR UNsuccessful %index% type non-delayed expansion ?

Is there a convenient way to escape this problem when in a loop and %index% is the pre-loop index value and I really need the latest !index! value

Regards
Alan

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Why does this fail, and how to fix it ? :- "%VAR_!A_2!%"

#2 Post by dbenham » 08 Dec 2011 13:16

alan_b wrote:Does CMD.EXE defer all !index! type delayed expansion until <after> successful OR UNsuccessful %index% type non-delayed expansion ?
I assumed you meant after
By Definition, that is exactly what delayed expansion is supposed to do.

I'm assuming your index value is in a block (perhaps a FOR loop), so %A_2% does not provide the current value.

You simply need to load the value into a FOR variable like so

Code: Select all

for /f %%N in ("!A_2!") do echo( "VAR_%%N" = "!VAR_%%N!"

It might be even easier if you can eliminate the A_n variable altogether by using a FOR /L loop variable as your index


Dave Benham

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why does this fail, and how to fix it ? :- "%VAR_!A_2!%"

#3 Post by alan_b » 08 Dec 2011 16:25

Thanks

I was not expecting anything logical to come out of CMD.EXE after you showed me that this failure

Code: Select all

set COMMAND=('type %2 ^| find "3" ')
...
<%1 (
  for /f %%i in !COMMAND! do (

simply needs the relocation of brackets in

Code: Select all

set COMMAND='type %2 ^| find "3" '
...
<%1 (
  for /f %%i in (!COMMAND!) do (

When CMD.EXE hits me with one "meaningless" failure,
I tend to think the next failure is also meaningless

You are not thinking large enough with one loop.
I have more loops than I can throw an abacus at :)

At the innermost level I have this loop

Code: Select all

:NEW_PROC
SET /A Ndx/=10 & SET /A Ndx+=1 & SET /A Ndx*=10 & SET "A_!Ndx!=Mode-%3" & SET /A Ndx+=1
<%1 (
  for /f %%i in (!COMMAND!) do (
    set "ln=" & set /p "ln=" & echo(!ln!
    SET /A Ndx+=1 & SET "A_!Ndx!=!ln!"
  )
)
GOTO :EOF

The above is the result of my recent topic, but with the addition that each line of text extracted by SET /P
is now not only echoed to StdOut (instant debug rather than redirecting to output file to be opened)
but in addition each line is now captured in a single indexed array of strings.

When :NEW_PROC ends then control returns to a higher level control loop,
and I temporarily inserted debug code that tested the content of the last line of text in A_!Ndx!
That is when I found failure using %A_!Ndx!% so on a wild guess I tried !A_%Ndx%! and got lucky.
Had I tried placing such debug code inside NEW_PROC then %Ndx% would have a stale value,
and only the !Ndx! value would be up-to-date, but then how do I access the value of A_!Ndx!

I really hoped a few crafty ^^ escape character or some other trick I can copy but not comprehend would do the job.
I will just need to be careful where I put my debug.

Regards
Alan

Post Reply