Page 1 of 1
Batch File to Replace String in Text File
Posted: 01 Dec 2010 08:07
by lostpenan
Hello,
I'm new to batch files. I need to create a batch file to replace a string in text file with the current directory of the file.
Basically search "FILEPATH" and replace with eg. C:\temp.
snippet from file....
<xsl:output method="html"/>
<xsl:variable name="BackgroundColor">buttonface</xsl:variable>
<xsl:variable name="BackgroundImage">url(FILEPATH)</xsl:variable>
<xsl:variable name="BackgroundPosition">bottom right</xsl:variable>
<xsl:variable name="BackgroundRepeat">no-repeat</xsl:variable>
Thanks for help.
Jon
Re: Batch File to Replace String in Text File
Posted: 01 Dec 2010 12:35
by aGerman
Because of the special characters (< and >) I suggest don't use pure batch for the replacement. Have a look at
this.
Regards
aGerman
Re: Batch File to Replace String in Text File
Posted: 01 Dec 2010 14:08
by jeb
Hi lostpenan,
this batch should work (secure also with < and >
![Smile :)](./images/smilies/icon_smile.gif)
)
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "search=FILEPATH"
set "replace=C:\temp"
del output.txt
for /F "delims=" %%a in (xml.tmp) DO (
set line=%%a
setlocal EnableDelayedExpansion
>> output.txt echo(!line:%search%=%replace%!
endlocal
)
hope it helps
jeb
Re: Batch File to Replace String in Text File
Posted: 02 Dec 2010 02:17
by lostpenan
Thanks Jeb. It works just the way I want it. Except that all the empty lines are gone.
My app seems very picky with the blank lines. It won't work without those lines.
Anyway to keep them intact?
Original...
Code: Select all
function clicker(a,b)
{
if (a.style.display =='')
{
a.style.display = 'none';
]]> b.src='<xsl:value-of select="CommonPath"/>/triangle.gif'; <![CDATA[
}
else
{
a.style.display='';
]]> b.src='<xsl:value-of select="CommonPath"/>/triangle2.gif'; <![CDATA[
window.external.UpdateCtrlTabOrder();
}
}
]]>
</xsl:comment><xsl:text> </xsl:text>
</SCRIPT><xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<SCRIPT language="JavaScript" FOR='window' EVENT='onunload'><xsl:text> </xsl:text>
<xsl:comment>
<![CDATA[
window.external.UnRegisterControls();
]]>
</xsl:comment><xsl:text> </xsl:text>
</SCRIPT><xsl:text> </xsl:text>
<xsl:text> </xsl:text>
</HEAD><xsl:text> </xsl:text>
After batch file... Empty lines are missing...
Code: Select all
function clicker(a,b)
{
if (a.style.display =='')
{
a.style.display = 'none';
]]> b.src='<xsl:value-of select="CommonPath"/>/triangle.gif'; <![CDATA[
}
else
{
a.style.display='';
]]> b.src='<xsl:value-of select="CommonPath"/>/triangle2.gif'; <![CDATA[
window.external.UpdateCtrlTabOrder();
}
}
]]>
</xsl:comment><xsl:text> </xsl:text>
</SCRIPT><xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<SCRIPT language="JavaScript" FOR='window' EVENT='onunload'><xsl:text> </xsl:text>
<xsl:comment>
<![CDATA[
window.external.UnRegisterControls();
]]>
</xsl:comment><xsl:text> </xsl:text>
</SCRIPT><xsl:text> </xsl:text>
<xsl:text> </xsl:text>
</HEAD><xsl:text> </xsl:text>
Re: Batch File to Replace String in Text File
Posted: 02 Dec 2010 02:47
by orange_batch
jeb + me
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "search=FILEPATH"
set "replace=C:\temp"
del output.txt
for /F "delims=] tokens=1*" %%a in ('type xml.tmp^|find /v /n ""') DO (
set line=%%b
if defined line (
setlocal EnableDelayedExpansion
>> output.txt echo(!line:%search%=%replace%!
endlocal
) else (
>> output.txt echo(
)
)
Should work. (
for skips blank lines. This works because it prepends [line number] to the beginning of all lines, so we delimit the ] and check for any trailing text.)
Keep in mind, as usual DOS will unavoidably append a line return to the end of the file.
PS: You can process a file's contents and rewrite itself by placing
^&type nul^>xml.tmp after
find /v /n "" (no need to use an output.txt if the contents are predictably safe to process)
Re: Batch File to Replace String in Text File
Posted: 02 Dec 2010 03:29
by jeb
@orange_batch
sorry but the code fails with the sample input file, because it strips the leading ]] in line 6.
The delims=] doesn't work if ] could be also the first char in the input file.
You could changed it to
line=!line:*]=! to remove the beginning.
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "search=FILEPATH"
set "replace=C:\temp"
del output.txt
for /F "delims=*" %%a in ('findstr /n $ xml.tmp') DO (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if defined line (
echo(!line:%search%=%replace%!
) else (
echo(
)
endlocal
)
hope it helps
jeb
Re: Batch File to Replace String in Text File
Posted: 26 Dec 2010 19:03
by ghostmachine4
lostpenan wrote:Hello,
I'm new to batch files. I need to create a batch file to replace a string in text file with the current directory of the file.
Basically search "FILEPATH" and replace with eg. C:\temp.
snippet from file....
<xsl:output method="html"/>
<xsl:variable name="BackgroundColor">buttonface</xsl:variable>
<xsl:variable name="BackgroundImage">url(FILEPATH)</xsl:variable>
<xsl:variable name="BackgroundPosition">bottom right</xsl:variable>
<xsl:variable name="BackgroundRepeat">no-repeat</xsl:variable>
Thanks for help.
Jon
Don't use batch (cmd.exe commands) to parse and manipulate files. Its not the correct tool for the job. Use vbscript if you want a native solution. However if you can download stuff, I recommend gawk. you can download
gawk for windows. And here's how to do it with 1 line
Code: Select all
C:\test>gawk "BEGIN{\"pwd\"|getline p} /FILEPATH/{ sub(\"FILEPATH\",p) }1" file
<xsl:output method="html"/>
<xsl:variable name="BackgroundColor">buttonface</xsl:variable>
<xsl:variable name="BackgroundImage">url(C:\test)</xsl:variable>
<xsl:variable name="BackgroundPosition">bottom right</xsl:variable>
<xsl:variable name="BackgroundRepeat">no-repeat</xsl:variable>
Re: Batch File to Replace String in Text File
Posted: 26 Dec 2010 19:10
by rfpd
It was cool if there was any batch code to replace text strings without download something, just pure batch. But however ghostmachine is true we can use visual basic script.
Re: Batch File to Replace String in Text File
Posted: 26 Dec 2010 19:47
by ghostmachine4
rfpd wrote:It was cool if there was any batch code to replace text strings without download something, just pure batch. But however ghostmachine is true we can use visual basic script.
nah, gawk.exe is just 1 executable file. You download it ONCE only, and you can bring anywhere. But if you only can use native stuff, the next better thing to batch is of course vbscript (and of course, better than that would be powershell)