0

I need to loop through the list of files set through the command line, and loop through a local array.

No knowing of a better way, I tried using labels, but cmd doesn't behave as expected:

@echo off 

if "%~1"=="" GOTO PARAM

setlocal enableextensions enabledelayedexpansion

set colors[0]="<style>yellow</style>"
set colors[1]="<style>blue</style>"

set COUNTER=0
for %%f in ("%1") DO (
    echo Handling %%f
    
    echo !COUNTER!

:LOOP
    IF !COUNTER!=="0" GOTO CASE_YELLOW
    IF !COUNTER!=="1" GOTO CASE_BLUE
    IF !COUNTER! GTR 6 GOTO END
    set /a COUNTER +=1
)
GOTO END

:CASE_YELLOW
ECHO Case YELLOW
GOTO LOOP

:CASE_BLUE
ECHO Case BLUE
GOTO LOOP

:PARAM
echo Usage : %0 myfile.xml/*.xml

:END
ECHO Done.

Here's the output using "myscript.bat file*.xml":

Handling file1.xml
0
Handling fileé.xml
1
Done.

Thank you.

Gulbahar
  • 5,343
  • 20
  • 70
  • 93
  • 1
    You cannot `goto` a label inside of a loop. – Gerhard Aug 30 '20 at 20:23
  • 1
    I do not see the point of the for loop either. Can you show what parameter you give to the commandline as `%1`? – Gerhard Aug 30 '20 at 20:27
  • Because !colors[%COUNTER%]! didn't work. It's "myscript.bat *.xml" – Gulbahar Aug 30 '20 at 20:49
  • @Gulbahar, unless you can explain what your issue is, other than the double variable expansion issue you had in yesterdays version of this code, and again in today's, please read, adapt, and use the advice you've already received. If you implement that advice, and your script still exhibits an issue, feel free to [edit your question](https://stackoverflow.com/posts/63661779/edit), and include the attempted implementation, and a full explanation of how it isn't producing the results you require of it. _You're not new to this site, and should know better than repeating your closed question._ – Compo Aug 30 '20 at 21:31

1 Answers1

1

Thankfully you put enough info in your code commenting to kinda see where you were going to with the arguments.

that said, if you want to do something when a condition is met in a loop, and continue with the loop after doing it, you simply need to change to calling your sub Functions, and then end of file which will return tou to your place in the loop.

this should do the needful


@(setlocal enableextensions enabledelayedexpansion
  echo off 
  set "colors[0]=<style>yellow</style>"
  set "colors[1]=<style>blue</style>"
  set /a "COUNTER=0"
  SET "SRC=%~1"
)

IF /I "%~1" EQU "" (
  Call :PARAM
) ELSE (
  CALL :Main
)

( ENDLOCAL
  CALL :End
  EXIT /B 0
)

:Main
  For %%f in ("%SRC%") DO (
    IF !COUNTER! < 6 (
       echo Handling %%f
       echo !COUNTER!
       CALL ECHO.IN MAIN LOOP COLOR= "%%Colors[%counter%]%%"
       IF !COUNTER!==0 CALL :CASE_YELLOW
       IF !COUNTER!==1 CALL :CASE_BLUE
       set /a "COUNTER+=1"
     )  ELSE (
       echo.exiting.
       GOTO :EOF
     )
  )
GOTO :EOF

:CASE_YELLOW
  ECHO Case YELLOW
  ECHO.In sub function yellow COLOR = "!Colors[%counter%]!"
GOTO :EOF

:CASE_BLUE
  ECHO Case BLUE
  ECHO.In sub function blue COLOR= "!Colors[%counter%]!"
GOTO :EOF

:PARAM
  echo Usage : %0 myfile.xml/*.xml
GOTO :EOF

:END
  ECHO Done.
  Here's the out
GOTO :EOF




Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • Ben, if you take a look at their [closed 'duplicate' question from yesterday](https://stackoverflow.com/q/63646484), they were not only directed to an answer which provides the solution, I provided two examples/fixes in my comment to assist them. – Compo Aug 30 '20 at 21:18
  • 1
    Because I didn't see your reply. Sorry about that. Thank you Ben. – Gulbahar Aug 30 '20 at 22:37