computing
  • 3

Solved Continue With Next File In a For-Loop

  • 3

Hi.

I have a folder containing several q_*.sos files.

In my program 3Q2PG.bat, I have a for-loop

...
for %%f in (%FILKATALOG%\q_*.sos) do (
...
)

Within this for-loop a textfile “feil_koordsys.txt” may be created (depending on if something is wrong in the particular q_*.sos-file).
If nothing is wrong, feil_koordsys.txt will not be created, and the rest of the loop should be carried out before continuing with the next q_*.sos file.
If soemthing is wrong, feil_koordsys.txt will be created, and the rest of the commands in the for-loop should be skipped and the program should begin on the top of the loop with the next q_*.sos file in the folder.

I have tried something like this:

 

echo off
setLocal enableDELAYedexpansion
...
<b>:loopen</b>
for %%f in (%FILKATALOG%\q_*.sos) do (
  ...
    if exist %PROG_HOME%\feil_koordsys.txt (
    del %PROG_HOME%\feil_koordsys.txt
    call :loopen
  )
  ...
)

This doesn’t work.

Nor does this:

echo off
setLocal enableDELAYedexpansion
...
for %%f in (%FILKATALOG%\q_*.sos) do (
<b>:loopen</b>
  ...
    if exist %PROG_HOME%\feil_koordsys.txt (
    del %PROG_HOME%\feil_koordsys.txt
    call :loopen
  )
  ...
)

Does anyone know how to do this?

I would appreciate any help.
Thanks.

Best regards
Anne, Norway

Share

1 Answer

  1. What you coded is wrong because call :label actually calls an internal subroutine and is not aimed to perform a jump. More goto :label is not allowed inside a for-loop since it disrupts the nominal control flow. So you have to code

    @echo off
    for %%f in (%FILKATALOG%\q_*.sos) do call :PROC %%f
    goto :EOF
    
    :PROC
    ...
    if exist "%PROG_HOME%\feil_koordsys.txt" (
      del "%PROG_HOME%\feil_koordsys.txt"
      goto :EOF
    )
    ...
    goto :EOF
    

    Inside the :PROC subroutine refer to your files .sos using the %1 parameter instead of %%f as the batch tail’s operands.

    :EOF is the built-in return point to the main or the OS and doesn’t need to be delared. About the setlocal EnableDelayedExpansion that is all another story with no contact witj your problem.

    • 0