【问题标题】:set nor working inside a for loop for windows dos cmd设置在 Windows dos cmd 的 for 循环中不起作用
【发布时间】:2013-11-21 18:39:22
【问题描述】:

我有一个脚本可以搜索某些目录并为给定目录设置路径列表,现在我想从中排除一些经常出现的不需要的目录。

这是脚本

@ECHO off

setlocal enableextensions
:start
set scriptname=%0%
set PAUSE_ON_ERROR=yes

rem #
rem ######   SECTION TO CUSTOMIZE   #####
rem #

rem #
rem # This is the list of directories where instances of TECSYS iTopia installed under 
rem # JBoss are located.
rem #

set jboss_dir_list=C: C:\TecsysDev\iTopiaControlPanel\trunk

rem #
rem ######   END SECTION TO CUSTOMIZE   #####
rem #

set argNb=0
for %%x in (%*) do Set /A argNb+=1
if %argNb% GTR 1 (
    goto :showUsage
) else if %argNb% == 0 (
    set ENV_NAME=*
) else (
    set ENV_NAME=*%~1*
)


:MainBlock
set scriptname=%0%
cd /d %~dp0
for /f "usebackq delims=" %%D in (`cd`) do set scriptdir=%%D

for /f "usebackq delims=" %%D in (`cd`) do set CURRENT_DIR=%%D
cd "%scriptdir%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
set JBOSS_DIR=%JBOSS_DIR%\..\..\..\..\..
cd "%JBOSS_DIR%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
cd "%CURRENT_DIR%"

rem Make sure that we have the findstr utility installed.

set findstr_found=
for /f "usebackq" %%f in (`echo x ^| findstr x 2^>nul`) do set findstr_found=%%f
if "X%findstr_found%" == "X" (
   echo The findstr utility is not found.
   goto pauseforError
)

call :getJbossVersion

rem prepare the list of special JBoss environments to exclude

if /i "%JBOSS_VERSION%" lss "5" (
    set env_to_exclude=all default minimal
) else if /i "%JBOSS_VERSION%" lss "6" (
    set env_to_exclude=all default minimal standard web
) else (
    set env_to_exclude=all default minimal jbossweb-standalone standard
)

rem find the environment directories
setlocal enabledelayedexpansion
Set Count=1
for  %%f in (%jboss_dir_list%) do ( 
    for /f "delims=" %%G in ('dir /b /ad "%%~f\jboss*"') do (
        if exist "%%f\%%G\server\%ENV_NAME%" (
            for /f "delims=" %%H in ('dir /b /ad "%%~f\%%~G\server\%ENV_NAME%"') do (
                echo count est !count!
                call :concat %%~f\%%~G\server\%%~H
                Set /A Count+=1
            )
        )
    )
)


rem echo %ENV_NAME%
rem  %jboss_home_list%

:concat

echo the environment is %1
set is_env_to_exclude=no

for  %%L in (%env_to_exclude%) do (
set is_env_to_exclude=no
    for /f "usebackq delims=" %%U in (`echo %1 ^| findstr %%L`) do (
        set is_env_to_exclude=yes               
        echo flag du Ellouze
    )
)
echo %is_env_to_exclude%

rem echo %is_env_to_exclude%
rem set jboss_home_list=%1 %jboss_home_list%
goto :eof

:getJbossVersion

for /f "usebackq tokens=2" %%v in (`echo. ^| %JBOSS_DIR%\bin\run.bat -V ^| ^ 
                                   findstr "JBoss" ^| findstr /i /v "BootStrap"`) do (
   set JBOSS_VERSION=%%v
)

goto :EOF

:showUsage

echo Usage:  tish [environment]
   echo                  ^|
   echo                  +--- installed environment


rem SET /P uname=Please enter your name: 
rem IF "%uname%"=="" GOTO Error
rem ECHO Hello %uname%, Welcome to DOS inputs!
rem GOTO End
rem :Error
rem ECHO You did not enter your name! Bye bye!!

:pauseforError
if "%PAUSE_ON_ERROR%" == "yes" pause
:End

我的想法是通过 :concat 子例程进行文件处理,问题是设置 is_env_to_exclude=yes 循环内的指令不起作用,当我执行脚本时显示回显标志但设置 is_env_to_exclude 始终设置没有。

【问题讨论】:

  • 您能否将您的示例简化为一个简短的脚本,该脚本在不依赖特定外部文件或目录树的情况下重现该问题?如果你说的是包含set is_env_to_exclude=yes 的循环,我认为delayedExpansion 不会有帮助,因为一开始就没有展开。
  • 你真的需要检查 findstr 是否存在吗?

标签: windows batch-file for-loop


【解决方案1】:

我认为你需要的是打破循环:

echo the environment is %1
set is_env_to_exclude=no

for  %%L in (%env_to_exclude%) do (
set is_env_to_exclude=no
    for /f "usebackq delims=" %%U in (`echo %1 ^| findstr %%L`) do (
        set is_env_to_exclude=yes               
        echo flag du Ellouze
        goto :break_loop
    )
)
:break_loop
echo %is_env_to_exclude%

在每次迭代%env_to_exclude% 项目时,is_env_to_exclude 设置为 no。 虽然代码对我来说有点复杂:)

【讨论】:

    【解决方案2】:

    这真的取决于env_to_exclude的内容。

    is_env_to_exclude会为每个%%L设置为no,所以即使设置为yes一次,如果设置后处理的元素较多,也会重新设置为no。

    set is_env_to_exclude=no
    

    在循环中似乎是罪魁祸首;删除它似乎可以解决问题。

    AAMOI、set "flag="set flag=Y 允许使用 if [not] defined flag,这具有额外的优势,即标志的当前状态在 FOR 循环中可用,而无需 enabledelayedexpansion

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      相关资源
      最近更新 更多