Batch files: List all files in a directory with relative paths
batch-file, cmd, relative-path, windows
Solution
You could simply get the character length of the current directory, and remove them from your absolute list
setlocal EnableDelayedExpansion
for /L %%n in (1 1 500) do if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
setlocal DisableDelayedExpansion
for /r . %%g in (*.log) do (
set "absPath=%%g"
setlocal EnableDelayedExpansion
set "relPath=!absPath:~%len%!"
echo(!relPath!
endlocal
)
Problem
Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list? For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do ``` for /r . %%g in (*.txt) do echo %%g >> C:\temp\test.txt ``` or ``` dir *.txt /b /s >> C:\temp\test.txt ``` and I will get something like ``` C:\test\Doc1.txt C:\test\subdir\Doc2.txt C:\test\subdir\Doc3.txt ``` If I do ``` for /r . %%g in (*.txt) do echo %%~nxg >> C:\temp\test.txt ``` I will get something like ``` Doc1.txt Doc2.txt Doc3.txt ``` But what I really want is: ``` Doc1.txt subdir\Doc2.txt subdir\Doc3.txt ``` Is it possible? If my post is too confusing: I basically want List files recursively in Linux CLI with path relative to the current directory, but just for Windows.