0

I am trying to print a variable in parenthesised code which is assigned a value using other variable in batch file.

Here is my code

@echo off
SETLOCAL enableDelayedExpansion
CALL initialize
CALL fun
:fun (
 @echo off
 Setlocal EnableDelayedExpansion
 Set "SOMEVAR=!OTHERVAR!"
 ECHO ..%SOMEVAR%
 EXIT /B 0
)
:initialize (
 set SOMEVAR=somevalue
EXIT /B 0
)

The output is just

..

How do i fix it so that i can assign value to somevar?

Edit1: If i now try to print in following way it does its job

ECHO ..!SOMEVAR!

But my script uses lot of %SOMEVAR%. Does that mean i need to change them all?

Note: Othervar is initialzed in other function and it does show proper value if it is echoed.

Abhi
  • 724
  • 1
  • 6
  • 15
  • Your code doesn't show how that `OTHERVAR` is assigned (it's empty by default naturally). Generally it should work as is even with quotes. – wOxxOm Aug 19 '15 at 17:57
  • @wOxxOm: actully its just a snippet from actual code which i am trying to edit. All the values are previously initialized. – Abhi Aug 19 '15 at 18:02
  • Well, your code works as expected here if I add a simple initializer or assign OTHERVAR in a loop. Divide your batch file and conquer the problem. – wOxxOm Aug 19 '15 at 18:05
  • 1
    Where is `OTHERVAR` echoed? within a different `setlocal`/`endlocal` block maybe, could that be? if so, type `setlocal /?` and `endlocal /?` and consult the help messages... – aschipfl Aug 19 '15 at 18:06
  • @aschipfl: If i add "echo !OTHERVAR!" Just after setlocal statement it shows the correct value which in my case would be c:\xyz – Abhi Aug 19 '15 at 18:09
  • 1
    Okay... I guess your code snippet is part of a compound statement, that is, a code portion enclosed in parantheses `()` -- right??; such is considered as a single command line; variables enclosed in `%%` are expanded to their values at parsing time, that is, when the line of code is _read_; so `set` works fine, but the expansion of `%SOMEVAR%` happens earlier... – aschipfl Aug 19 '15 at 18:16
  • 1
    How about commenting out or removing `@echo off` to see if any helpful information appears? – lit Aug 19 '15 at 20:44
  • @aschipfl: you are right. My code is in parenthesis so i guess i need to change all my %SOMEVAR% expansions to !SOMEVAR!. Can you please put that as an answer so that i can check it as correct one? – Abhi Aug 20 '15 at 05:59
  • 1
    Putting it as an answer makes only sense if you update your question so that the parenthesised code is comprised... currently it would rather cause confusion than help other programmers... – aschipfl Aug 20 '15 at 08:35
  • how does the q sounds now? – Abhi Aug 20 '15 at 10:09
  • You have the classical [delayed expansion](http://stackoverflow.com/questions/25324354/windows-batch-files-what-is-variable-expansion-and-what-does-enabledelayedexpa/25328044#25328044) problem... – Aacini Aug 20 '15 at 10:23
  • @Aacini: yep i am done with the problem!!! – Abhi Aug 20 '15 at 10:53

2 Answers2

1

Since the code portion containing echo %SOMEVAR% is in between parenthesis, the variable is expanded before being set (consult this post for a good explanation).

There are the following options to avoid that:

  1. to expand it like !SOMEVAR! (delayed expansion), or
  2. to avoid the parenthesis:

    @echo off
    SETLOCAL EnableDelayedExpansion
    CALL initialize
    CALL fun
    exit /B
    
    :fun
    Setlocal EnableDelayedExpansion
    Set "SOMEVAR=!OTHERVAR!"
    ECHO ..%SOMEVAR%
    EXIT /B 0
    
    :initialize
    set SOMEVAR=somevalue
    EXIT /B 0
    

    Note the additional exit /B in the above code snippet after the call statements, which prevents from falling into the code below unintentionally.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Does this work any closer to your expectations? Note that SOMEVAR will not be returned to your shell environment unless an ENDLOCAL block is used.

C:>set OTHERVAR=0123456789

C:>type g2.bat
@echo off
SETLOCAL enableDelayedExpansion

CALL:initialize
CALL:fun
EXIT /B 0

:fun (
    rem @echo off
    Setlocal EnableDelayedExpansion
    Set "SOMEVAR=!OTHERVAR!"
    ECHO ..%SOMEVAR%
    GOTO :EOF
)

:initialize (
    set SOMEVAR=somevalue
    GOTO :EOF
)

C:>g2.bat
..0123456789
lit
  • 14,456
  • 10
  • 65
  • 119
  • Hi @Paul: it works when its the only thing in file but my code is in parenthesis so %% wont work since those are evaluated at compile time as opposed to !! which is done at run time. So there is no option than to use !! for all my usage :D. Anyways thanks for trying that out! – Abhi Aug 20 '15 at 05:59