14

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?

5
  • Hint: Search for ENABLEDELAYEDEXPANSION Commented Sep 20, 2012 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. Commented Sep 20, 2012 at 18:10
  • @dbenham - :-) You're dead right! If I get only one cent per this type of question ... Commented Sep 20, 2012 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 Commented Sep 20, 2012 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. Commented Sep 20, 2012 at 20:13

1 Answer 1

34

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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!
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.
Extra emphasis on the !variableName! exclamation point syntax.
Alternate solution is a batch function: stackoverflow.com/a/13809834/733092
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.