Search code examples
windowsloopsbatch-filecmdprompt

CMD/Batch Prompting for Input loop without goto command


I have a loop scanning for the files of a folder and I want to ask for each file if it should be moved or not. As I am already in that loop I can't use goto commands.

How can I prompt a input loop (yes / no) without using a goto command?

@echo off
setlocal enableDelayedExpansion

FOR %%a in (*.jpg) DO (

:: ask
set /p Input="Should !a! be moved? (yes/no)"

if %Input% == 'yes' (
MOVE !a! path/
)

if %Input% == 'no' (
:: just move on in loop
)

:: if not yes or no goback to ask

)

Solution

  • You can't do this directly because of the way the FOR loop is processed. You can do it indirectly though. Look for the call-command:

    echo off
    setlocal
    
    for %%i in (*) do
        call :Bar %%i
    goto :eof
    
    :Bar  
    @echo %1 
    goto :eof 
    
    
    :eof    
    endlocal