tg94
tg94

Reputation: 47

Batch File: For Loop not Running in Multiple Nested Loops

I am trying to find all files in a directory with a certain file extension. This is the code I have been testing:

@echo off
SETLOCAL enabledelayedexpansion

REM For each line in the specified file
for /f "tokens=1" %%a in (%1.txt) do (

  REM Get the string of letters before the first number appears
  for /f "tokens=1 delims=0123456789" %%G in ("%%a") do (

    REM Determine if a file of the desired type is in the directory:
    if EXIST C:\Users\User1\%%G\%%a\Folder1\*.txt (
        echo file present

        REM Search through the directory for all files with that extension and list them
        for /r C:\Users\User1\%%G\%%a\Folder1\ %%i in (*.txt) do (
            echo %%i  ******<---This is the loop that isn't happening**
        )
     )
  echo.
  )
)

It operates as expected and echoes "file present" when there is a file there, but doesn't list all the files. When I put a blank echo in that loop, it doesn't do it, so somehow that loop is not running and I don't know why.

I have the exact same for loop in another batch file that works fine. I'm just trying to adapt it for a different purpose, and I can't figure out why it's not running. Any insight is greatly appreciated!

Upvotes: 1

Views: 61

Answers (1)

Magoo
Magoo

Reputation: 80023

Ah - the old for /r with variable root trap

    pushd C:\Users\User1\%%G\%%a\Folder1
    for /r  %%i in (*.txt) do (
        echo %%i
    )
    popd

should fix it.

Upvotes: 1

Related Questions