设置不在Windows dos cmd的for循环内工作

时间:2013-11-21 18:39:22

标签: windows batch-file for-loop

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

这是脚本

@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 forrt中的forrt循环不起作用,当我执行脚本时,echo标志显示但是set is_env_to_exclude总是被设置没有。

2 个答案:

答案 0 :(得分: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。 虽然代码对我来说有点复杂:)

答案 1 :(得分:0)

这实际上取决于env_to_exclude

的内容 对于每个is_env_to_exclude

%%L将设置为“否”,因此即使将其设置为yes一次,如果在设置后处理了更多元素,也会重新设置 - 设为否。

set is_env_to_exclude=no
循环中的

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

AAMOI,set "flag="set flag=Y允许if [not] defined flag,它具有额外的优势,即FOR循环内的标志的CURRENT状态可用而不需要{{1} }。