Page 1 of 1

Replacing backslash with slash

Posted: 15 Feb 2009 21:13
by JubaJuba
awhile back I received some help here with a script the wrote the contents of a directory/subdirectories in their relative path. Here's the code for that;

Code: Select all

@ECHO off
del data.xml
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%c in ('dir *.jpg /a-d /ogn /b /s') do (
 set abspath=%%~fc
 call set "relpath=%%abspath:%cd%\=%%"
 rem echo abs=!abspath! rel=!relpath!
 echo ^<img^>!relpath!^<^/img^> >> data.xml
)
( ENDLOCAL & REM RETURN VALUES


It works properly, however I ran into a snag implementing it. Turns out I can't use backslashes (\) but forward slashes (/) will work just fine. I'm not sure how to go about doing this with the script above, I thought about having an external batch file execute after "data.xml" is written and having it go through and replace the backslashes, but seems like there must be a easier/better way to do it.

Re: Replacing backslash with slash

Posted: 15 Feb 2009 21:36
by mhubers
Hi,
What the format you looking to get?

Something like this?

c:\somedir\somemoredir\pic.jpg
to
<img>c:/somedir/somemoredir/pic.jpg</img>

If so then i have just the thing for you.

Mark




JubaJuba wrote:awhile back I received some help here with a script the wrote the contents of a directory/subdirectories in their relative path. Here's the code for that;

Code: Select all

@ECHO off
del data.xml
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%c in ('dir *.jpg /a-d /ogn /b /s') do (
 set abspath=%%~fc
 call set "relpath=%%abspath:%cd%\=%%"
 rem echo abs=!abspath! rel=!relpath!
 echo ^<img^>!relpath!^<^/img^> >> data.xml
)
( ENDLOCAL & REM RETURN VALUES


It works properly, however I ran into a snag implementing it. Turns out I can't use backslashes (\) but forward slashes (/) will work just fine. I'm not sure how to go about doing this with the script above, I thought about having an external batch file execute after "data.xml" is written and having it go through and replace the backslashes, but seems like there must be a easier/better way to do it.

Posted: 15 Feb 2009 22:07
by mhubers
@ECHO off
del data.xml
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%c in ('dir *.bat /a-d /ogn /b /s') do (
set abspath=%%~fc
call set "relpath=%%abspath:%cd%\=%%"
rem echo abs=!abspath! rel=!relpath!
set relpath=!relpath:\=/!
echo ^<img^>!relpath!^<^/img^>
)


Add "set relpath=!relpath:\=/!" to that script.
I hope that give you what your looking for.

Mark

Posted: 15 Feb 2009 22:15
by JubaJuba
Thank you so much! That's exactly what I needed.