1

I have a script that eill search for certain directories and set a list of paths for the given directories, now i want to exclude from them some recurrent unwanted ones.

Here's the script

@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

My idea is to to do the filetring through the :concat subroutine, problem is set is_env_to_exclude=yes insrtuction inside the for loop isn't working, when i execute the script the echo flag is displaying but the set is_env_to_exclude is always set to no.

user2966439
  • 307
  • 1
  • 5
  • 14
  • Can you reduce your example to a short script that reproduces the problem with no dependencies on specific external files or directory trees? If you're talking about the loop that contains `set is_env_to_exclude=yes`, I don't think delayedExpansion will help, since there's no expansion going on there in the first place. – Michael Burr Nov 21 '13 at 19:41
  • do you really need to check if findstr exists? – npocmaka Nov 21 '13 at 19:49

2 Answers2

1

I think what you need is to break the loop:

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%

as on each iteration over %env_to_exclude% items the is_env_to_exclude is set to no. Although the code is a little bit complicated for me :)

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

It really depends on the contents of env_to_exclude.

is_env_to_exclude will be set to no for each %%L, so even if it is set to yes once, if there are more elements processed after it's been set, it will be re-set to no.

The

set is_env_to_exclude=no

in the loop seems to be the culprit; removing it would seem to fix the problem.

AAMOI, set "flag=" and set flag=Y allows if [not] defined flag which has the added advantage that the CURRENT status of the flag is available within a FOR loop without needing enabledelayedexpansion.

Magoo
  • 77,302
  • 8
  • 62
  • 84