Batch File to Replace String in Text File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lostpenan
Posts: 2
Joined: 01 Dec 2010 07:54

Batch File to Replace String in Text File

#1 Post by lostpenan » 01 Dec 2010 08:07

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

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to Replace String in Text File

#2 Post by aGerman » 01 Dec 2010 12:35

Because of the special characters (< and >) I suggest don't use pure batch for the replacement. Have a look at this.

Regards
aGerman

jeb
Expert
Posts: 849
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Batch File to Replace String in Text File

#3 Post by jeb » 01 Dec 2010 14:08

Hi lostpenan,

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

lostpenan
Posts: 2
Joined: 01 Dec 2010 07:54

Re: Batch File to Replace String in Text File

#4 Post by lostpenan » 02 Dec 2010 02:17

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>&#10;</xsl:text>
</SCRIPT><xsl:text>&#10;</xsl:text>

<xsl:text>&#10;</xsl:text>
<SCRIPT language="JavaScript" FOR='window' EVENT='onunload'><xsl:text>&#10;</xsl:text>
<xsl:comment>
<![CDATA[
  window.external.UnRegisterControls();
]]>
</xsl:comment><xsl:text>&#10;</xsl:text>
</SCRIPT><xsl:text>&#10;</xsl:text>
<xsl:text>&#10;</xsl:text>

</HEAD><xsl:text>&#10;</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>&#10;</xsl:text>
</SCRIPT><xsl:text>&#10;</xsl:text>
<xsl:text>&#10;</xsl:text>
<SCRIPT language="JavaScript" FOR='window' EVENT='onunload'><xsl:text>&#10;</xsl:text>
<xsl:comment>
<![CDATA[
  window.external.UnRegisterControls();
]]>
</xsl:comment><xsl:text>&#10;</xsl:text>
</SCRIPT><xsl:text>&#10;</xsl:text>
<xsl:text>&#10;</xsl:text>
</HEAD><xsl:text>&#10;</xsl:text>

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch File to Replace String in Text File

#5 Post by orange_batch » 02 Dec 2010 02:47

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)

jeb
Expert
Posts: 849
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Batch File to Replace String in Text File

#6 Post by jeb » 02 Dec 2010 03:29

@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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch File to Replace String in Text File

#7 Post by ghostmachine4 » 26 Dec 2010 19:03

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>

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Batch File to Replace String in Text File

#8 Post by rfpd » 26 Dec 2010 19:10

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.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Batch File to Replace String in Text File

#9 Post by ghostmachine4 » 26 Dec 2010 19:47

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)

Post Reply