View unanswered posts | View active topics It is currently 19 Sep 2014 09:49



Post new topic Reply to topic  [ 9 posts ] 
Batch File to Replace String in Text File 
Author Message

Joined: 01 Dec 2010 07:54
Posts: 2
Post 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


01 Dec 2010 08:07
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1772
Location: Germany
Post 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


01 Dec 2010 12:35
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 673
Location: Germany
Post Re: Batch File to Replace String in Text File
Hi lostpenan,

this batch should work (secure also with < and > :) )
Code:
@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


01 Dec 2010 14:08
Profile

Joined: 01 Dec 2010 07:54
Posts: 2
Post 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...
Code:

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:
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>


02 Dec 2010 02:17
Profile
Expert

Joined: 01 Aug 2010 17:13
Posts: 442
Location: Canadian Pacific
Post Re: Batch File to Replace String in Text File
jeb + me

Code:
@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)


02 Dec 2010 02:47
Profile WWW
Expert

Joined: 30 Aug 2007 08:05
Posts: 673
Location: Germany
Post 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.

Code:
@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


02 Dec 2010 03:29
Profile

Joined: 12 May 2006 01:13
Posts: 315
Post 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:
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>


26 Dec 2010 19:03
Profile

Joined: 06 Jul 2009 16:19
Posts: 78
Location: Lisbon, Portugal
Post 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.


26 Dec 2010 19:10
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post 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)


26 Dec 2010 19:47
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.