View unanswered posts | View active topics It is currently 31 Oct 2014 13:51



Post new topic Reply to topic  [ 8 posts ] 
Improved BatchSubstitute.bat 
Author Message
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Improved BatchSubstitute.bat
Hi,

the current BatchSubstitute.bat has several limitations

- Lines starting with "]" character will end up empty
- OldStr must not start with "*", "~" or "%", must not contain "="
- NewStr must not contain "%"
- Lines must not contain any of the following characters within a quoted string: "&<>|^",
- It is very slow

The new one have also limitations:
- OldStr [in] - string to be replaced, must not begin with "*", "~" or "!", must not contain =
- NewStr [in] - string to replace with, must not contain !

But:
- A line can begin with every character
- special characters and quoted strings are working always
- it is faster

Code:
@echo off

REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

:BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::    In OldStr and in NewStr, two quotes are reduced to a single quote, so you can search for a single quote
::          OldStr [in] - string to be replaced, must not begin with "*" or "!", must not contain "="
::          NewStr [in] - string to replace with, must not contain "!"
::          File   [in] - file to be parsed
set "param1=%~1"
set "param2=%~2"
if defined param1 set "param1=%param1:""="%"
if defined param2 set "param2=%param2:""="%"
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "delims=" %%A in ('"findstr /n ^^ %3"') do (
   set "line=%%A"
   setlocal EnableDelayedExpansion

   set "line=!line:*:=!"
   if defined line (
      set "line=!line:%param1%=%param2%!"
      (echo(!line!)
   ) ELSE echo(
   endlocal
)


EDIT 20.12.10:
Add the quote handler, so replacing of single quotes are possible
Replace the $ with ^^ in findstr, so it also finds the last line of a file

hope it helps
jeb


Last edited by jeb on 20 Dec 2010 05:42, edited 2 times in total.



10 Dec 2010 05:16
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1800
Location: Germany
Post Re: Improved BatchSubstitute.bat
Good job!

I understand how the file works excepting one issue: What's the reason why you enclosed echo(!line! in brackets?

Regards
aGerman


11 Dec 2010 08:35
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Re: Improved BatchSubstitute.bat
Hi aGerman,

the
Code:
(echo(!line!)

The enclosing parenthesis are only to ensure, that after !line! is no extra/invisible space.


hope it helps
jeb (theOtherGerman)


11 Dec 2010 13:15
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1800
Location: Germany
Post Re: Improved BatchSubstitute.bat
Yeah I see. Good idea, jeb. I do the same with double quotes and the SET command.

Regards
aGerman (oneOfSeveralGermansHere)


11 Dec 2010 13:54
Profile
Expert

Joined: 01 Aug 2010 17:13
Posts: 442
Location: Canadian Pacific
Post Re: Improved BatchSubstitute.bat
I was wondering if there would be good reason to write a comprehensive package of functions to extend DOS, like jQuery for Javascript.

Of course it seems, the more support added to a function, the slower it becomes. For example, if this script was combined with one to replace characters like =... Even worse, char-by-char iterations are the safest way to do so, though I don't suggest using DOS to process varying text complexities rather than file/folder paths. It would be wiser to switch to PowerShell. :?


13 Dec 2010 04:01
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: Improved BatchSubstitute.bat
jeb wrote:

The new one have also limitations:
- OldStr [in] - string to be replaced, must not begin with "*", "~" or "!", must not contain =
- NewStr [in] - string to replace with, must not contain !

jeb

why waste your time when in the end you still have that many constraints? For example, besides the oldstr must not contain "*" "~" or "!" and
newstr cannot contain "!" , what if i want to replace exact words?
Code:
C:\test>more file
this is a line
this is another line end

C:\test>test.bat "this is a" "test" file
test line
testnother line end


As you can see, I only want to replace "this is a" , and not "this is another". With a specialized tool like sed, one can set regex to do this
Code:
C:\test>sed "s/\<this is a\>/test/" file
test line
this is another line end


Use a proper file replacement tool, and it has been developed already.


26 Dec 2010 19:27
Profile

Joined: 10 Sep 2013 21:54
Posts: 1
Post Re: Improved BatchSubstitute.bat
Hi .. I have just found BATCHSUBSTITUE.BAT and was hoping that it would/will solve my problem...
I am attempting to use a batch file to edit a .reg file to replace the NIC speed entry
The original cant handle the entry in the .reg files due to entries like "="6"
The along came the references to the update.. sounds great.....but
replacing the old .bat with the new .bat and it doesn't run... has the entry format changed ??

I am using the following syntax that does some of the work on the old version
Quote:
C:\MORSEDHD\REG\BatchSubstitute.bat ""6"" "2" C:\MORSEDHD\REG\speedlow.reg>c:\morsedhd\reg\up.reg


So .. help .. Many thanks


10 Sep 2013 22:16
Profile

Joined: 10 Feb 2012 02:20
Posts: 4330
Post Re: Improved BatchSubstitute.bat
A helper batch file called repl.bat from - viewtopic.php?f=3&t=3855 will help to replace quotes etc.

\x22 is a quote (ascii value in HEX).


11 Sep 2013 03:45
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: Bing [Bot] and 13 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.