Nested for loops, not quite working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cpjust
Posts: 2
Joined: 14 Apr 2014 18:33

Nested for loops, not quite working

#1 Post by cpjust » 14 Apr 2014 18:57

I'd like to run some code on each of the following categories "Monthly Weekly Daily Smoke_Test Smoke_Machine" except for any of those words are in the ExcludeList variable.
The following code is a simple example I tried, but even though Weekly & Daily are in the ExcludeList, everything gets printed. It's like the %exclude% variable is always true.
How can I make this work?

Code: Select all

@ECHO OFF

SET ExcludeList=Weekly,Daily

FOR %%i IN (Monthly Weekly Daily Smoke_Test Smoke_Machine) DO (
    SET exclude=false
    FOR %%j IN (%ExcludeList%) DO IF "%%j"=="%%i" SET exclude=true
    IF NOT "%exclude%"=="true" (
        ECHO *** Include "%%i"
    )
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Nested for loops, not quite working

#2 Post by Ed Dyreen » 14 Apr 2014 19:44

Code: Select all

@ECHO OFF &setlocal enableDelayedExpansion

SET ExcludeList=Weekly,Daily

FOR %%i IN (Monthly Weekly Daily Smoke_Test Smoke_Machine) DO (
    SET exclude=false
    FOR %%j IN (%ExcludeList%) DO IF "%%j"=="%%i" SET exclude=true
ECHO IF NOT "!exclude!"=="true"
    IF NOT "!exclude!"=="true" (
        ECHO *** Include "%%i"
    )
)

pause
exit

Code: Select all

IF NOT "false"=="true"
*** Include "Monthly"
IF NOT "true"=="true"
IF NOT "true"=="true"
IF NOT "false"=="true"
*** Include "Smoke_Test"
IF NOT "false"=="true"
*** Include "Smoke_Machine"
Druk op een toets om door te gaan. . .

Code: Select all

CMD /?

...
Delayed environment variable expansion is NOT standard enabled. You can enable or disable a particular invocation of CMD.EXE with the / V: ON or / V: OFF. You can complete all invocations of CMD.EXE on or off at a computer and / or user logon session by setting one or both of the following REG_DWORD values ​​in the registry to 0x1 or 0x0 using REGEDT32.EXE:

     HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Command Processor \ DelayedExpansion

         and / or

     HKEY_CURRENT_USER \ Software \ Microsoft \ Command Processor \ DelayedExpansion
...

cpjust
Posts: 2
Joined: 14 Apr 2014 18:33

Re: Nested for loops, not quite working

#3 Post by cpjust » 14 Apr 2014 20:42

Thank you! That works perfectly.
I'll definitely have to remember that SETLOCAL enableDelayedExpansion trick.

Post Reply