0

Everything before the for loop works fine, i can add "pause" to it and it pauses, but inside the for loop it does not work. I have tried to add pause but it doesn't, and the pause after the loop does not occur.

@echo off
setlocal enabledelayedexpansion

setlocal

for %%g in ( * ) do (
pause

    echo %%g

    set strEnd=___EndOfString___
    set strFilename=%%g
    set strFile=!strFilename!!strEnd!
    echo !strFile!
    pause
    :loop

    set strChar=!strFile:~0,1!
    set strFile=!strFile:~1!
    if !strChar!==a echo A
    pause
    if not !strFile!==!strEnd! goto loop
)

pause
endlocal
jrwygal
  • 27
  • 4
  • Remove the `@echo off` and see if it Displays any errors. – Tomas Dittmann Aug 09 '16 at 05:49
  • Just tried that, nothing showed up, the cmd window popped up then disapeared – jrwygal Aug 09 '16 at 05:55
  • Try executing it from a commandprompt then. – Tomas Dittmann Aug 09 '16 at 06:21
  • 4
    Windows command interpreter does not support labels within a __FOR__ loop, use a subroutine. Open a command prompt window and run `call /?` for help on how to code a subroutine. Testing batch files should be always done by running the batch file from within a command prompt window. Reason: On opening the command prompt window `cmd.exe` is started with parameter `/K ` (Keep open) while on double clicking on a batch file `cmd.exe` is started with parameter `/C` (close). So with a double click you usually don't see the output error message. – Mofi Aug 09 '16 at 06:51
  • 3
    And remove the second `setlocal` in your code. Why? Please read [this answer](http://stackoverflow.com/a/38676582/3074564). `setlocal` always makes the 4 stack pushes and creates a new environment table independent on being used without or with 1 or 2 of the 4 optional parameters. Run in a command prompt window `setlocal /?` for help on this command. – Mofi Aug 09 '16 at 06:58

1 Answers1

1

This construct should work. You can adjust timeouts duration.

@echo off
setlocal enabledelayedexpansion

for %%g in ( * ) do (echo %%g
    set strEnd=___EndOfString___
    set strFilename=%%g
    set strFile=!strFilename!!strEnd!
    echo !strFile!
    timeout 2 >nul
    call :loop
)
pause
exit /b

:loop
:: add more code here
timeout 2 >nul
exit /b

However, your original script inside the :loop is not correct, and needs to be changed depending on the batch purpose.

sambul35
  • 1,058
  • 14
  • 22