Improved BatchSubstitute.bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Improved BatchSubstitute.bat

#1 Post by jeb » 10 Dec 2010 05:16

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
Last edited by jeb on 20 Dec 2010 05:42, edited 2 times in total.

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

Re: Improved BatchSubstitute.bat

#2 Post by aGerman » 11 Dec 2010 08:35

Good job!

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

Regards
aGerman

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

Re: Improved BatchSubstitute.bat

#3 Post by jeb » 11 Dec 2010 13:15

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)

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

Re: Improved BatchSubstitute.bat

#4 Post by aGerman » 11 Dec 2010 13:54

Yeah I see. Good idea, jeb. I do the same with double quotes and the SET command.

Regards
aGerman (oneOfSeveralGermansHere)

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

Re: Improved BatchSubstitute.bat

#5 Post by orange_batch » 13 Dec 2010 04:01

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

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

Re: Improved BatchSubstitute.bat

#6 Post by ghostmachine4 » 26 Dec 2010 19:27

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.

southernwave
Posts: 1
Joined: 10 Sep 2013 21:54

Re: Improved BatchSubstitute.bat

#7 Post by southernwave » 10 Sep 2013 22:16

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Improved BatchSubstitute.bat

#8 Post by foxidrive » 11 Sep 2013 03:45

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).

Post Reply