Batch script for loop won't set variable

依然范特西╮ 提交于 2019-11-26 11:32:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12518242/batch-script-for-loop-wont-set-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!