SET variable not working in an 'if' or 'for' loop of windows batch script

View: New views
3 Messages — Rating Filter:   Alert me  

SET variable not working in an 'if' or 'for' loop of windows batch script

by vij_guy102 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

inside an 'if' block or 'for' loop, the SET variable is not
substituted. I had to finally use the GOTO labels to do away with 'if'
block. I tried CMD /V:ON to enable the delayed execution, but the
script won't go beyond that.

Any solution to this?


Re: SET variable not working in an 'if' or 'for' loop of windows batch script

by foxidrive-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 18 Feb 2008 22:17:58 -0000, "vij_guy102" <vij_guy102@...>
wrote:

>inside an 'if' block or 'for' loop, the SET variable is not
>substituted. I had to finally use the GOTO labels to do away with 'if'
>block. I tried CMD /V:ON to enable the delayed execution, but the
>script won't go beyond that.
>
>Any solution to this?

@echo off
setlocal enableddelayedexpansion

and use !variable! inside the block instead of %variable%


Or call a subroutine to set the variable outside the block.



Re: SET variable not working in an 'if' or 'for' loop of windows batch script

by storemike :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Aha...thanks for the solution!  I was having the same problem.  FOr
anyone who needs it, here's my code (for parsing a tab-delimited
file):

 
FOR /F "usebackq tokens=1,2* delims=%tabspaces%" %%a in (%1) DO (
        call :replacem %%a %%b
)


:replacem
set aa=%1
set bb=%2
set aa=%aa:"=%
set bb=%bb:"=%
echo stuff here


Hope that helps someone.

Mike

--- In batchworld@..., foxidrive <foxidrive@...> wrote:
>
> On Mon, 18 Feb 2008 22:17:58 -0000, "vij_guy102" <vij_guy102@...>
> wrote:
>
> >inside an 'if' block or 'for' loop, the SET variable is not
> >substituted. I had to finally use the GOTO labels to do away
with 'if'

> >block. I tried CMD /V:ON to enable the delayed execution, but the
> >script won't go beyond that.
> >
> >Any solution to this?
>
> @echo off
> setlocal enableddelayedexpansion
>
> and use !variable! inside the block instead of %variable%
>
>
> Or call a subroutine to set the variable outside the block.
>