1

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!

1
  • Please provide examples of what is in the input text file at the beginning of your job. Commented Dec 1, 2017 at 20:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Magoo not understanding your explanation. I have never had a FOR /R not work when using the path as an option. In what scenarios does it fail?
@Squashman : Where the path option cannot be resolved at parse-time, in my experience.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.