Page 1 of 1

Improved BatchSubstitute.bat

Posted: 10 Dec 2010 05:16
by jeb
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: Select all

@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

Re: Improved BatchSubstitute.bat

Posted: 11 Dec 2010 08:35
by aGerman
Good job!

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

Regards
aGerman

Re: Improved BatchSubstitute.bat

Posted: 11 Dec 2010 13:15
by jeb
Hi aGerman,

the

Code: Select all

(echo(!line!)

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


hope it helps
jeb (theOtherGerman)

Re: Improved BatchSubstitute.bat

Posted: 11 Dec 2010 13:54
by aGerman
Yeah I see. Good idea, jeb. I do the same with double quotes and the SET command.

Regards
aGerman (oneOfSeveralGermansHere)

Re: Improved BatchSubstitute.bat

Posted: 13 Dec 2010 04:01
by orange_batch
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. :?

Re: Improved BatchSubstitute.bat

Posted: 26 Dec 2010 19:27
by ghostmachine4
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: Select all

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

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.

Re: Improved BatchSubstitute.bat

Posted: 10 Sep 2013 22:16
by southernwave
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
C:\MORSEDHD\REG\BatchSubstitute.bat ""6"" "2" C:\MORSEDHD\REG\speedlow.reg>c:\morsedhd\reg\up.reg


So .. help .. Many thanks

Re: Improved BatchSubstitute.bat

Posted: 11 Sep 2013 03:45
by foxidrive
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).