1

I have a directory that can contain over a hundred files. I want to create a loop that will go through each file in that directory and ask the user if they want to process the next file or exit the script all together. I can't seem to get the prompt to work inside for the For Loop. Any thoughts would be greatly appreciated.

@echo off
Color 1F

SetLocal EnableExtensions EnableDelayedExpansion 

Echo File Processing Screen
Echo.

:UserPrompt1
Set /p "strContProc=Are you sure you wish to process these files? (Y/N) %=%"

If /i "%strContProc%" == "y" (
Goto ProcFiles

) Else If /i "%strContProc%" == "n" (
Goto ProcCancel1

) Else (
Goto UserPrompt1
)

:ProcFiles
If exist "%userprofile%\Desktop\PCExport\Disable" (

Set /a numTotFile = 0
Set /a numFileCount = 0

For /r %%f in (*.txt) do (
Set /a numTotFile += 1
)

) ELSE (
Goto DirNotFound
)

If %numTotFile% EQU 0 (
    Goto NoFilesFound

    ) ELSE If %numTotFile% EQU 1 (
    Echo Processing file !numFileCount! of %numTotFile%...
    Goto EndProc

    ) ELSE (
    Echo.
    Echo %numTotFile% Files will now be processed.
    Echo.
)

:ReturnProc
For /r %%f in (*.txt) do (
Set /a numFileCount += 1
Echo Processing file !numFileCount! of %numTotFile%. . .
Echo.
Echo !numFileCount! >> %%f

If !numFileCount! EQU %numTotFile% (
Echo !numFileCount! of %numTotFile% files have been processed.  
Goto EndProc
)   

:UserPrompt2
Set /p "strNextProc=Do you wish to process the next file? (Y/N)  %=%"

If /i "%strNextProc%" == "y" (
Goto ReturnProc

    ) Else If /i "%strNextProc%" == "n" (
    Goto ProcCancel2

    ) Else (
    Goto UserPrompt2
)

)

Goto EndProc

:DirNotFound
Echo The specified processing directory could not be found!
Goto EndProc

:NoFilesFound
Echo Processing cannot continue. There were no export files found to process.
Goto EndProc

:ProcCancel1
Echo.
Echo Processing of files was canceled at the user's request.
Goto EndProc

:ProcCancel2
Echo.
Echo Processing of files was canceled at the user's request. Please make a note of the last file that was processed.
Goto EndProc

:EndProc
EndLocal
Echo.
Pause
0

Issuing a GOTO within the body of a FOR loop immediately terminates the loop.

A simple solution is to write a :subroutine outside of the loop that contains the GOTO, and call the :subroutine from within the loop.

The pseudo code would be something like the following:

for ... in (...) do (
   Possibly do some processing
   call :userPrompt
   Possibly do some more processing with !var!
)
exit /b

:userPrompt
set "var="
set /p "var=Your prompt
if ...InvalidInput... goto :userPrompt
exit /b
  • But exit /b will just leave the subroutine, to propagate the decision to leave I'd exit /b 1 and attach a conditional execution on fail || to the call :userprompt || exit /b 1 – LotPings Apr 5 '19 at 5:11
  • @LotPings - For an expected binary input, sure. But my pseudo code is intended to be more generic. – dbenham Apr 5 '19 at 12:18
  • 0 Placing the prompt as a subroutine did what I had hoped but caused the For Loop to reset to the first file; never advancing to the subsequent files. I understand why this occurred since the GOTO immediately terminates the loop. How do I advance to the next file? – Steven W. Apr 8 '19 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.