Isabelle
Isabelle

Reputation: 33

How to exit a for /f loop in a batch ?

I have a question about a batch file that I am writing.

How can I define my "for /f" loop to execute only one part of my script and not the whole script ?

In the script below, I would like

I precise that I need to use a "call" method (because of another issue with backlash).

Thank you for your help :)

    @echo off
  setlocal enabledelayedexpansion
  WMIC LOGICALDISK where drivetype=3 get caption>%~n0.tmp
  for /f "tokens=1-3 skip=1" %%a in ('type "%~n0.tmp"') do call :displayinfo %%a
del "%~n0.tmp"
goto :eof

:displayinfo
set drive=%1
echo Le drive est %drive%
echo lancement du DIR
REM call dir /A HS /s /b %drive%\ >> d:\Dir_ALL.txt
echo Fin du DIR

:step2
echo this is the step2, to be executed when the for /F loop is over.
echo blablalblablalballbabal

:step_End
echo Ths is the end
@pause

Upvotes: 1

Views: 6259

Answers (1)

aphoria
aphoria

Reputation: 20179

You need to make two changes.

  1. Change the goto :eof two lines below your for line to goto :step2.
  2. Add goto :EOF as the last line of your :displayinfo method. This will cause the method to return to the for loop.

It should look like this.

@echo off
setlocal enabledelayedexpansion
WMIC LOGICALDISK where drivetype=3 get caption>%~n0.tmp
for /f "tokens=1-3 skip=1" %%a in ('type "%~n0.tmp"') do call :displayinfo %%a
del "%~n0.tmp"
goto :step2

:displayinfo
set drive=%1
echo Le drive est %drive%
echo lancement du DIR
REM call dir /A HS /s /b %drive%\ >> d:\Dir_ALL.txt
echo Fin du DIR
goto :EOF

:step2
echo this is the step2, to be executed when the for /F loop is over.
echo blablalblablalballbabal

:step_End
echo Ths is the end
@pause

Upvotes: 4

Related Questions