View unanswered posts | View active topics It is currently 23 May 2013 06:28



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

Joined: 30 Aug 2007 08:05
Posts: 567
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: 1395
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: 567
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: 1395
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: 308
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
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

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