DO loop inside of a FOR loop?

Oct 14, 2015
Something tickles the back of my mind that a DO loop inside of a FOR loop isn't allowed. And, my attempts at making said combo work seem to support that. Am I correct about this? If I'm not, can someone help me figure out why this isn't working?

        for /f "tokens=*" %%i in (@%grouplist) do (
        echo    %%i...
        set groupdn=%@replace[dn: ,,%@execstr[ls-full "%%i" dn:]]
        set lr=0
        set hr=1499
        if exist "temp_%%i.txt" del /q "temp_%%i.txt"
        do forever

            echo %lr,%hr
            set startsize=%@filesize["temp_%%i.txt"]
            c:\OpenLdap\bin\ldapsearch.exe -o ldif-wrap=no -D "xxxxx" -w "yyyyy" -b "%groupdn" -H "ldap://ldaphost.ext" "(objectclass=*)" member;range=%lr-%hr 2>nul | findstr /i /b /c:"member;range" >> "temp_%%i.txt"
            if %startsize=%@filesize["temp_%%i.txt"] leave
            set lr=%@eval[ %lr + 1500 ]
            set hr=%@eval[ %hr + 1500 ]

Essentially I have a source file of AD groups (grouplist.txt). I need to loop through that list with a FOR and inside that FOR, do multiple ldap queries on the active group with a DO loop because the groups could have thousands of user and you can only retrieve them in 1500 count chunks.

The result I'm seeing is that the FOR only loads the first group in the list and then exits the FOR block when it hits the DO and continues the rest of the batch file. I haven't even been able to test that the DO loop works with my current syntax because the batch terminates before the DO.

Thanks for the feedback.
Last edited:
Oct 14, 2015
Thanks Rex. I'm a creature of habit and for years I've used FOR when I wanted to loop through a file's contents. I switched over to DO i in %@grouplist and it worked like a charm. Thanks for the advice and be well.

Similar threads