DosTips.com

A Forum all about DOS Batch
It is currently 26 May 2016 14:21

All times are UTC-06:00




Post new topic  Reply to topic  [ 9 posts ] 
Author Message
PostPosted: 01 Dec 2010 08:07 
Offline

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


Top
   
PostPosted: 01 Dec 2010 12:35 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1987
Location: Germany
Because of the special characters (< and >) I suggest don't use pure batch for the replacement. Have a look at this.

Regards
aGerman


Top
   
PostPosted: 01 Dec 2010 14:08 
Offline
Expert

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


Top
   
PostPosted: 02 Dec 2010 02:17 
Offline

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


Top
   
PostPosted: 02 Dec 2010 02:47 
Offline
Expert

Joined: 01 Aug 2010 17:13
Posts: 442
Location: Canadian Pacific
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)


Top
   
PostPosted: 02 Dec 2010 03:29 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 782
Location: Germany
@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


Top
   
PostPosted: 26 Dec 2010 19:03 
Offline

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


Top
   
PostPosted: 26 Dec 2010 19:10 
Offline

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


Top
   
PostPosted: 26 Dec 2010 19:47 
Offline

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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 9 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Matt Williamson and 11 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® Forum Software © phpBB Limited