Dear Guys,
I am trying to convert/replace a part in a long txt file.
<icon>http://www.example.com/img.jpg</icon> to <icon src="img.jpg" />
I managed to replace everything except signs like < > = using this code:
@echo off
setlocal enabledelayedexpansion
set "filename=C:\ProgramData\example.txt"
set "tempfile=C:\ProgramData\output.txt"
for /F "delims=" %%a in (%filename%) do (
set text=%%a
set text=!text:^http://www.example.com/=!
echo !text!>>%tempfile%
)
Does someone know how to improve that code in an easy way? Because as soon as I use this code I get an error: set text=!text:^<icon=<icon src="!
Melanie
Help with string replace
Moderator: DosItHelp
Re: Help with string replace
SED is a commonly tool used for text manipulations, using regular expressions.
It could be done using WSH/VBS too and regular expressions.
Did you want to replace one specific set of text or every occurrence, and is the name always img.jpg?
To do it in plain batch will take some extra steps and knowing the makeup of the file is usually important.
It could be done using WSH/VBS too and regular expressions.
Did you want to replace one specific set of text or every occurrence, and is the name always img.jpg?
To do it in plain batch will take some extra steps and knowing the makeup of the file is usually important.
Re: Help with string replace
it has many occurences and the names of the images changes, however <icon>http://www.example.com/ stays the same and </icon> too. I would like to do it in batch since it is just a part of a batch programm
Re: Help with string replace
This seems to work here.
Code: Select all
@echo off
echo ^<icon^>http://www.example.com/img.jpg^</icon^>>example.txt
echo ^<icon^>http://www.example.com/img234.jpg^</icon^>>>example.txt
echo ^<icon^>http://www.example.com/123img.jpg^</icon^>>>example.txt
echo ^<icon^>http://www.example.com/imgbbb.jpg^</icon^>>>example.txt
echo ^<icon^>http://www.example.com/imgaaa.jpg^</icon^>>>example.txt
@echo off
setlocal enabledelayedexpansion
set "filename=C:\ProgramData\example.txt"
set "tempfile=C:\ProgramData\output.txt"
set "filename=example.txt"
set "tempfile=output.txt"
for /F "delims=" %%a in (%filename%) do (
set "text=%%a"
set "text=!text:<icon>http://www.example.com/=!"
set "text=!text:</icon>=!"
set text=^<icon src="!text!" /^>
>>%tempfile% echo !text!
)
Re: Help with string replace
it works!! I was missing the ^
many thanks^^
many thanks^^