Replacing backslash with slash

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Replacing backslash with slash

#1 Post by JubaJuba » 15 Feb 2009 21:13

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.

mhubers
Posts: 4
Joined: 15 Feb 2009 21:29
Location: Madison, WI.

Re: Replacing backslash with slash

#2 Post by mhubers » 15 Feb 2009 21:36

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.

mhubers
Posts: 4
Joined: 15 Feb 2009 21:29
Location: Madison, WI.

#3 Post by mhubers » 15 Feb 2009 22:07

@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

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

#4 Post by JubaJuba » 15 Feb 2009 22:15

Thank you so much! That's exactly what I needed.

Post Reply