0

I'm a newbie the commands of a Batch Script on Windows
I have a question about a batch file that I'm writing.
How to break from the "for /l" loop ?

In the script below, I would like to copy x number files from one folder to another
However, after the break the first loop, the second loop doesn't works fine,
it copied all files in the source folder to the destination folder.

Thank you for your help :)

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

SET Source=C:\Data
SET Target1=C:\Work\bak1
SET Target2=C:\Work\bak2
SET Target3=C:\Work\bak3

SET /A days=13
SET /A weekNum=9
SET /A MaxLimit=3
SET /A Count=0

for /l %%x in (1, 1, %days%) do (
    if %%x LEQ 9 (
        FOR /F %%G IN ('DIR /B "%Source%"\*.*') DO (
            copy /y "%Source%"\"%%G" "%Target1%"
            set /a Count=Count+1
            if !Count!==%MaxLimit% call :step1
        )
        :step1
REM         SET /A "x=x + 1"
REM         echo %%x
        
        IF %%x LEQ 1 IF %weekNum% EQU 9 ( 
            FOR /F %%G IN ('DIR /B "%Source%"\*.*') DO (
                copy /y "%Source%"\"%%G" "%Target2%"
                set /a Count=Count+1
                if !Count!==%MaxLimit% call :step2
            )
            :step2
REM             SET /A "x=x + 1"
REM             echo %%x
        )
    ) else (
REM         Copy file to Target3
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • You can find the answer here [exiting-out-of-a-for-loop-in-a-batch-file](https://stackoverflow.com/questions/6728798/exiting-out-of-a-for-loop-in-a-batch-file) – Sangeerththan Balachandran Sep 23 '21 at 06:31
  • That article I have also read, I have tried with `goto` and `call` but for some reason it still doesn't work, and using `exit` will completely exit the loop. Don't know where I'm doing wrong... – MkuRter Sep 23 '21 at 06:45
  • Did you check with exit /b – Sangeerththan Balachandran Sep 23 '21 at 06:50
  • if using `exit /b` i noticed that the processing will finish, not doing the 2nd loop to copy the file – MkuRter Sep 23 '21 at 06:54
  • You cannot use labels and `goto` in loops or other (parenthesised) blocks of code, because they break the loop/block context. `call` resumes execution upon returning from the called section, so this is likely not what you want… – aschipfl Sep 23 '21 at 08:09
  • Also, `SET /A "x=x + 1"` (which could be simplified to `set /A "x+=1"`) does not affect the `for` meta-variable `%%x`, it only modifies the *environment* variable `%x%`, which is something completely different… – aschipfl Sep 23 '21 at 08:32
  • Thank you for your reply! If I don't use `goto`, `call`, `exit /b` to break the loop, what should I do to break it? :( – MkuRter Sep 23 '21 at 08:45
  • You are already using `if %%x LEQ 9`. Have you considered `if !Count! leq %MaxLimit% (do loop code)` in the same way? – Stephan Sep 23 '21 at 10:02
  • You cannot use :labels in `FOR` loop body (strictly speaking, [inside a parenthesized code block](https://stackoverflow.com/a/32147995/3439404)) however you **can** use `goto :somewhere` if `:somewhere` is a label **outside** a parenthesized code block (`FOR` body). – JosefZ Sep 23 '21 at 14:33
  • ... to be clear: outside of *all* code blocks, if they are nested. – Stephan Sep 23 '21 at 18:34
  • Thank everybody for your kind guidance! So with my above way is not possible, I will do it in each loop separately – MkuRter Sep 24 '21 at 01:33

0 Answers0