0

Recursively list all files with .txt extension from the current folder printing the file name and only the relative folder path

In a bat file, I am trying to recursively list all files with .txt extension from the current folder printing the file name and only the relative folder path.

I have tried many solutions, the closest one to what I want is this:

@REM :treeProcess
@REM FOR %%f in (*.jmx) do echo %%f
@REM     FOR /D %%d in (*) do (
@REM         cd %%d
@REM         call :treeProcess
@REM         cd ..
@REM     )

But this prints the abosolute path and I need the relative path.

Example structure of what I need:

Current directory is C:\test

File a.txt
Folder 1
    File a1.txt
    Folder 1.1
        File a2.txt
        Folder 1.2
    

Expected result:

a.txt
Folder 1\a1.txt
Folder 1\Folder 1.1\File a2.txt
Mofi
  • 46,139
  • 17
  • 80
  • 143
JustNatural
  • 375
  • 7
  • 19

1 Answers1

1

Try this in a bat file that you will execute from the C:\test\ folder.

SET "StartPath=%cd%"

setlocal enableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!\*.txt"') DO (
    set "SubDirsAndFiles=%%f"
    set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
    IF EXIST "!StartPath!\!SubDirsAndFiles!\" (
        REM Echo This is a folder...
        ECHO !SubDirsAndFiles!\
    ) ELSE (
        REM Echo This is a file...
        ECHO !SubDirsAndFiles!
    )
)