I want to loop recursively through a directory and get the file names with full file path and additionally with path relative to the base folder path. For example, a file located in D:\foo\bar\a\b\file.txt has the relative path bar\a\b\.
I’m using this code right now which does not work as expected:
FOR /R D:\Download\758_DATA\ %%F IN (*) DO (
SET B = %%~pF
Set B=%B:~-6% ::take substring of the path
ECHO.%B%
ECHO %%F
ECHO.
)
I’m not sure what you want to accomplish, but at least this will give you a good starting point:
Since you’re modifying a variable within a loop, you need to tell the command interpreter that you want to allow variables to be "expanded with delay". This is what the setlocal enabledelayedexpansion is for. Then you can use
!as a variable delimiter instead of%, and variables written as such will be expanded at runtime. This is necessary because theforloop will be called like it is one single call. (You can see this when you leave out theecho off.)Edit: Revised example which includes automatic cut off: