I was also looking for function to list the relative path only.
Here some alternative solutions...
(Please replace %cd% with your start path, e.g. C:\download\folder )
List all sub-directories and files with relative path to a specific start path:
@echo off
SET "StartPath=%cd%"
SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!"') DO (
set "SubDirsAndFiles=%%f"
set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
ECHO !SubDirsAndFiles!
)
List selected file types only with the relative path to a specific start path (e.g. *.jpg, *.png):
@echo off
SET "StartPath=%cd%"
SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!\*.jpg" "!StartPath!\*.png"') DO (
set "SubDirsAndFiles=%%f"
set "SubDirsAndFiles=!SubDirsAndFiles:%StartPath%=!"
ECHO !SubDirsAndFiles!
)
List all sub-directories and files with relative path to a specific start path (paths ends with \):
@echo off
SET "StartPath=%cd%"
SetLocal EnableDelayedExpansion
FOR /f "tokens=*" %%f in ('dir /B /ON /S "!StartPath!"') 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!
)
)