12

I have a batch script trying to execute out of anthill to get the folder names containing plsql to be compiled.

for /F %%a in ('dir /b D:\AHP_WorkDir\var\jobs\projects\rprt_test\rprt_test\plsql') do (
  set FOLDER=%%a
  echo *** PROCESSING FOLDER %FOLDER% ***
)

This echos * PROCESSING FOLDER *

as if the variable is not getting set, which I'm pretty sure is true after spending way too long on verifying it

So...What am I doing wrong?

dbenham
  • 127,446
  • 28
  • 251
  • 390
Denis DuQuaine
  • 123
  • 1
  • 1
  • 4
  • Hint: Search for `ENABLEDELAYEDEXPANSION` – adarshr Sep 20 '12 at 18:08
  • 2
    There are many many MANY existing questions dealing with this same problem, and they all have a similar answer. The answer can be found within the help system - type `HELP FOR` or `FOR /?` from the command line. Hint - look for a discussion of Delayed Expansion. – dbenham Sep 20 '12 at 18:10
  • @dbenham - :-) You're dead right! If I get only one cent per this type of question ... – jeb Sep 20 '12 at 18:15
  • yes and I have read them and tried enabling delayed expansion, didn't get results so I ruled it out, sorry for wasting your time – Denis DuQuaine Sep 20 '12 at 18:40
  • oops - my bad. The relavent help is under `HELP SET`, not `HELP FOR`. And sorry if my prior comment came across as harsh. Obviously many people struggle to find the answer on their own, otherwise the question would not be so common! But I think that is more a reflection of the esoteric nature of batch and the lack of good documentation. It just becomes tiresome for those of us that regularly provide answers on this forum. – dbenham Sep 20 '12 at 20:13

1 Answers1

30

This is essentially a duplicate of a question asked earlier today. Here's my answer from said question...

You'll want to look at the EnableDelayedExpansion option for batch files. From the aforementioned link:

Delayed variable expansion is often useful when working with FOR Loops. Normally, an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.

So your script would end up looking something like this:

@echo off
setlocal enabledelayedexpansion

for /F %%a in ('dir /b D:\AHP_WorkDir\var\jobs\projects\rprt_test\rprt_test\plsql') do (
  set FOLDER=%%a
  echo *** PROCESSING FOLDER !FOLDER! ***
)

As an alternative, just use the %%a variable in your inner loop, rather than creating a new variable.

Community
  • 1
  • 1
Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
  • 1
    Thanks Jonah! I think this worked. I didn't have echo off in my first attempt to use enabledelayedexpansion...since I was trying to echo the value back to see if it was set, ah the circles we run in! – Denis DuQuaine Sep 20 '12 at 18:49
  • Glad to help! Don't forget to mark the answer as accepted, if it helped. That way future visitors to this question can benefit from your experience. – Jonah Bishop Sep 20 '12 at 18:54
  • Extra emphasis on the !variableName! exclamation point syntax. – E.S. Jan 18 '16 at 20:42
  • Alternate solution is a batch function: http://stackoverflow.com/a/13809834/733092 – Noumenon Jan 29 '17 at 17:31
  • @Jonah Bishop - Can you tell me if the "setlocal enabledelayedexpansion" is required only once per batch script or for each for loop in the batch script? I have multiple for loops in my batch file that I am working on and only one of the four loops isn't passing a value I am collecting. I did not research this question just because your posting helped me see that I might need to change my script a bit. However, it is helpful to know the answer. – RadFox Feb 08 '17 at 04:35
  • The `setlocal` directive will remain in effect until the script finds either an `endlocal` directive, or the end of the script is reached. So, in essence, you only need one at the top of the script. – Jonah Bishop Feb 15 '17 at 18:21