Batch File to Replace String in Text File
Moderator: DosItHelp
Batch File to Replace String in Text File
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
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
Because of the special characters (< and >) I suggest don't use pure batch for the replacement. Have a look at this.
Regards
aGerman
Regards
aGerman
Re: Batch File to Replace String in Text File
Hi lostpenan,
this batch should work (secure also with < and > )
hope it helps
jeb
this batch should work (secure also with < and > )
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
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...
After batch file... Empty lines are missing...
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>
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Batch File to Replace String in Text File
jeb + me
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)
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
@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.
hope it helps
jeb
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch File to Replace String in Text File
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
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch File to Replace String in Text File
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)