0

I have a simple bat script as follows:

for %%f in (DW_*.csv) do (
 echo f is.....: %%f
 set nam=%%f
 echo nam is ...: %nam%
)

The problem is the variable I am calling nam IS being set, but is NOT being echo'ed properly (it seems like it does NOT actually get set)

Can someone help me?

ps: I already tried enabling delayed expansion, but I still do not get expected results (see red arrow)

enter image description here

Ok the solution was indeed related to delayed expansion. Since this appears to be a common issue, I will leave this semi-duplicated post online with the following note: delayed expansion uses this syntax for referencing !variables! not this %syntax%

So this code worked and provides expected results:

setlocal enabledelayedexpansion
for %%f in (DW_*.csv) do (
 echo f is.....: %%f
 set nam=%%f
 echo nam is ...: !nam!
)

enter image description here

Doc
  • 435
  • 1
  • 5
  • 15
  • 2
    [delayed expansion problem](https://stackoverflow.com/a/30284028/2152082) – Stephan Sep 05 '18 at 15:41
  • 1
    Please search for 'delayed expansion', you should consider enabling it. This is probably the most common issue on this particular part of the site and should not be answered as a result. I would also urge you to delete your question accordingly. – Compo Sep 05 '18 at 15:43
  • I had already tried to enable delayed expansion yesterday before I posted. However, I am likely still missing something, as I did not get expected results. I have editing the post to show the code and the output – Doc Sep 06 '18 at 17:34
  • Come to find out, it was indeed related to delayed expansion, I simply needed to use !nam! instead of %nam%. Now I get expected results. Thank you all for solving this issue. – Doc Sep 06 '18 at 17:41

0 Answers0