Skipping over content in a for loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Skipping over content in a for loop?

#1 Post by SIMMS7400 » 04 Aug 2017 14:30

Hi Folks -

Is there a way to jump over a section of code in a for loop?

Here is my for loop:

Code: Select all

SET "PREP=Start"
SET "US06=HCT_ACT_06"
SET "US07=HCT_ACT_07"
SET "US08=HCT_ACT_08"
SET "UK09=HCT_ACT_09"
SET "EU10=HCT_ACT_10_PD"
SET "USHFM=HCT_ACT_HFM_0608"

SET "ENTITY_ALL=%US06%$%US07%$%US08%$%UK09%$%EU10%$%USHFM%"

::-- Invoke VB Script Usage Details --::
   ::-- cscript <VB Script Name> "Argument0" "Argument1" "Argument2" "Argument3" "Argument4" Argument5"
      ::-- Argument0 = HC_EXCEL_PREPPATH --::
      ::-- Argument1 = HC_EXCEL_SUBPATH  --::
      ::-- Argument2 = HC_TEMP_UTIL_WB   --::
      ::-- Argument3 = HC_UTIL_WB        --::
      ::-- Argument4 = HC_ACT_ABBREV     --::
      ::-- Argument5 = HC_Utility Tab    --::
      ::-- Argument6 = Excel File .Ext  --::
   
      ::-- All arguments are passed to the VB Script dynamically - no changes are required to VB Script ever --::

SET "NF5="
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%H IN ( %PREP% %ENTITY_ALL% ) DO (
   CLS
   CSCRIPT %HC_SF_VBS% "%HC_EXCEL_PREPPATH%" "%HC_EXCEL_SUBPATH%" "%HC_TEMP_UTIL_WB%" "%HC_UTIL_WB%" "%HC_ACT_ABBREV%_%%H.%EXT%" %%H .%EXT% && (
      
         IF "%%H"=="Start" ECHO Completed ^: Prepare Main Utility File >>"%LOGFILE%"
         IF "%%H" NEQ "Start" (
            ECHO Completed ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            )
         ) || (
         IF "%%H"=="Start" (
            ECHO Failed    ^: Prepare Main Utility File >>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         ) ELSE (
            ECHO Failed    ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         )
         )
PING -n 2 127.0.0.1>nul
)


However, based on a if check, I need to jump over the main peice of code in the for loop if false.

For instance, this is how I want to write it:

Code: Select all

FOR %%H IN ( %PREP% %ENTITY_ALL% ) DO (
   FOR %%E IN ( %ENTITY_ALL:$= % ) DO (
      IF %%E==%K09% GOTO SKIP
      IF %%E==%K09% GOTO SKIP   
   )
   CLS
   CSCRIPT %HC_SF_VBS% "%HC_EXCEL_PREPPATH%" "%HC_EXCEL_SUBPATH%" "%HC_TEMP_UTIL_WB%" "%HC_UTIL_WB%" "%HC_ACT_ABBREV%_%%H.%EXT%" %%H .%EXT% && (
      
         IF "%%H"=="Start" ECHO Completed ^: Prepare Main Utility File >>"%LOGFILE%"
         IF "%%H" NEQ "Start" (
            ECHO Completed ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            )
         ) || (
         IF "%%H"=="Start" (
            ECHO Failed    ^: Prepare Main Utility File >>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         ) ELSE (
            ECHO Failed    ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         )
         )
PING -n 2 127.0.0.1>nul
:SKIP
)


But it doesn't seem to be working. Am I doing anything wrong or is this the wrong approach?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Skipping over content in a for loop?

#2 Post by aGerman » 04 Aug 2017 14:56

You cant jump to a label inside of a loop.
Just use reverse logic. Instead of (pseudo code)
If a==b Or c==d Then skip seqence
use
If Not a==b And Not c==d Then execute seqence

Steffen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Skipping over content in a for loop?

#3 Post by penpen » 04 Aug 2017 14:58

Another possibility is to use a helper variable:

Code: Select all

FOR %%H IN ( %PREP% %ENTITY_ALL% ) DO (
   set "skip="
   FOR %%E IN ( %ENTITY_ALL:$= % ) DO (
      IF %%E==%K09% set "skip=true"
      IF %%E==%K09% set "skip=true"
   )
   if defined skip (
      CLS
      CSCRIPT %HC_SF_VBS% "%HC_EXCEL_PREPPATH%" "%HC_EXCEL_SUBPATH%" "%HC_TEMP_UTIL_WB%" "%HC_UTIL_WB%" "%HC_ACT_ABBREV%_%%H.%EXT%" %%H .%EXT% && (
     
         IF "%%H"=="Start" ECHO Completed ^: Prepare Main Utility File >>"%LOGFILE%"
         IF "%%H" NEQ "Start" (
            ECHO Completed ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            )
         ) || (
         IF "%%H"=="Start" (
            ECHO Failed    ^: Prepare Main Utility File >>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         ) ELSE (
            ECHO Failed    ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         )
         )
PING -n 2 127.0.0.1>nul
   )
)


penpen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Skipping over content in a for loop?

#4 Post by SIMMS7400 » 06 Aug 2017 08:10

Thank you both so miuch!

Helper variable worked great - I use that elsewhere. Totally blanked on using it within my for loop.

Thanks again!

Post Reply