0

I am writing a bat file, and one of the commands need a project path and a file path relative to the project.

For example

awesomecommand.exe project=C:\MyProject file="myfiles\file1.fbx"

I am iterating through all files in C:\MyProject (where my .bat file is located):

for /R "%~dp0" %%f in (*.fbx) do (
    ....
)

and I need to get the %%f path relative to %dp0. So if %%f = C:\MyProject\myfiles\file1.fbx and %~dp0="C:\MyProject", I need the result to be "myfiles\file1.fbx".

How would I do this?

aphoria
  • 19,796
  • 7
  • 64
  • 73
David Menard
  • 2,261
  • 3
  • 43
  • 67

2 Answers2

1
setlocal enabledelayedexpansion
for ... (
    set P=%%f
    set P=!P:%~dp0=!
    echo !P!
)

will remove the startup directory from P. (%~dp0 contains a trailing slash)

ths
  • 2,858
  • 1
  • 16
  • 21
0

xcopy command can be used to retrieve the list of files with relative paths

pushd "%~dp0"
for /f "tokens=1,* delims=\" %%a in ('xcopy "." "%temp%" /s /l ^| find "\"') do (
    echo %%b
)
popd

The list of files retrieved in in the format

.\folder\file.ext
.\file.ext
....

The for command delims and tokens handle the removal of the starting characters to leave the format requested

MC ND
  • 69,615
  • 8
  • 84
  • 126