Relative path for subdirectories from dir command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JubaJuba
Posts: 5
Joined: 04 Jan 2009 14:40

Relative path for subdirectories from dir command?

#1 Post by JubaJuba » 04 Jan 2009 14:50

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]

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 04 Jan 2009 18:15

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

JubaJuba
Posts: 5
Joined: 04 Jan 2009 14:40

#3 Post by JubaJuba » 04 Jan 2009 23:59

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.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 05 Jan 2009 17:05

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

JubaJuba
Posts: 5
Joined: 04 Jan 2009 14:40

#5 Post by JubaJuba » 05 Jan 2009 19:34

Thank you so much! I really appreciate it, I was getting so frustrated!

Post Reply