Batch script for loop won’t set variable

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.

Leave a Comment