Solved Continue with next file in a for-loop

Dell / Optiplex 960
February 1, 2012 at 07:29:40
Specs: Microsoft Windows XP Professional, 2.992 GHz / 3325 MB
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


See More: Continue with next file in a for-loop

Report •

#1
February 1, 2012 at 08:23:05
Don't use GOTO in FOR loops. The behavior is unsupported, undefined, and subject to change without prior warning.

Your best bet would be to separate the logic you want skipped into a function, then decide if you should CALL that function.

How To Ask Questions The Smart Way


Report •

#2
February 1, 2012 at 08:32:56
✔ Best Answer
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.


Report •

#3
February 2, 2012 at 04:16:53
Dear Razor2.3 and IVO,

thanks for your quick and helpful replies.

I followed IVO suggestions and it works. Thanks for mentioning the need of changing %%f to %1 inside the subroutine - saved me a lot of time. :-)
The purpose of setlocal EnableDelayedExpansion and ! vs % I have (more or less) figured out earlier. ;-)

Have a nice day!

Best regards
Anne, Norway


Report •
Related Solutions


Ask Question