Page 1 of 1

Relative path for subdirectories from dir command?

Posted: 04 Jan 2009 14:50
by JubaJuba
I'm trying to make a batch that will make a list of all the images in a folder, and all its subdirectories. However, as this requires /s with the dir command the entire path is listed. I would like just the relative path listed.

Here is the current script;

Code: Select all

setlocal enabledelayedexpansion
del files.txt
echo ^<images^> >> files.txt
for /F "tokens=*" %%c in ('dir *.jpg;*.bmp /a-d /ogn /b') do (
 echo ^<img^>%%c^<^/img^> >> list.xml
)
for /F "tokens=*" %%a in ('dir /ad /ogn /b /s') do (
 for /F "tokens=*" %%b in ('dir "%%a\*.jpg;*.bmp" /a-d /ogn /b') do (
  echo ^<img^>%%a^\%%b^<^/img^> >> list.xml
 )
)
echo ^<^/images^> >> files.txt
endlocal


I'm not sure how to go about doing this, I was thinking that trimming the current directory path from %%a before it's written to list.xml would be the easiest way, but I can't seem to workout how exactly to do it.

I was hoping someone might know how to solve this problem, thanks in advanced.[/i]

Posted: 04 Jan 2009 18:15
by jeb
Hi JubaJuba,

look at :MakeRelative from the Function Library, use it with an absolute path to your file and it should work.

Code: Select all

for /F "tokens=*" %%c in ('dir *.jpg;*.bmp /a-d /ogn /b') do (
 set filename=%%~fc
 call :MakeRelative filename
 echo ^<img^>!filename!^<^/img^> >> list.xml
)

jeb

Posted: 04 Jan 2009 23:59
by JubaJuba
After reading your reply I've tried to use the :MakeRelative function, but what is actually written to the file is the same as before, and now the batch just gets hung up in a loop =/

I was wondering why something like wouldn't work;

Code: Select all

for /F "tokens=*" %%c in ('dir *.jpg;*.bmp /a-d /ogn /b /s') do ( 
 set relpath=%%~fc
 call set "relpath=%%relpath%:!cd!=%"
 echo ^<img^>!relpath!^<^/img^> >> data.xml
)


Hell, I even tried using the BatchSubstitute.bat to remove the current path using %cd%... but that doesn't seem to work either.

I really have no clue what to do at this point, seems like it should be relatively simple, but I don't even know how many hours upon hours I've wasted on this.

Posted: 05 Jan 2009 17:05
by jeb
Hi,

you got it, but you missed some few characters.

I tested this and it seems to works.

Code: Select all

setlocal enabledelayedexpansion

for /F "tokens=*" %%c in ('dir *.bmp /a-d /ogn /b /s') do (
 set abspath=%%~fc
 call set "relpath=%%abspath:%cd%\=%%"
 rem echo abs=!abspath! rel=!relpath!
 echo ^<img^>!relpath!^<^/img^>
)


jeb

Posted: 05 Jan 2009 19:34
by JubaJuba
Thank you so much! I really appreciate it, I was getting so frustrated!