usage of Find/Replace utility (BatchSubstitute.bat)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pawan.shukla03
Posts: 2
Joined: 10 May 2009 02:18

usage of Find/Replace utility (BatchSubstitute.bat)

#1 Post by pawan.shukla03 » 10 May 2009 02:23

usage of Find/Replace utility (BatchSubstitute.bat) its code is :
@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
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
if "%*"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)

Length of record in my input file is 2600 bytes. but the above utility is only reading 1032 bytes of record and ignoring the rest. Kindly suggest how can i make it to read whole 2600 length record for find and replace.

please help...

thank you

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 11 May 2009 13:44

Well, the "find" command is the culprit. Use findstr instead like this:

Code: Select all

for /f "usebackq tokens=1,* delims=:" %%A in (`type %3^|findstr /n ".*"`) do (

pawan.shukla03
Posts: 2
Joined: 10 May 2009 02:18

#3 Post by pawan.shukla03 » 24 May 2009 02:03

but its limit of input record lenght is just 2048 bytes
my input file is 2600 byte long. :(

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 28 May 2009 14:21

Hmm. Both find and findstr appear to be limited in their ability to parse. You can just do this (untested):

for /f "usebackq delims=" %%A in ("%~3") do (
set "line=%%A"
call echo %%line:%~1=%~2%%
)

But blank lines will be skipped. You could get creative and use the find /n /v command to figure out which line numbers are blank in one for loop, and then use a simple counter in a 2nd for loop like I have above to trigger when you should output a blank echo. Here's what I mean:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set idx=0
for /f "usebackq tokens=1,2 delims=[]" %%a in (`type "%~3" ^| find /n /v ""`) do (
   set tmp=%%b
   if not defined tmp (
      set /a idx+=1
      set "blankline!idx!=%%a"
   )
)
set idx=1
set count=1
for /f "usebackq delims=" %%A in ("%~3") do (
   call set "nextblankline=blankline!idx!"
   call set "nextblankline=%%!nextblankline!%%"
   if 1!nextblankline!==1!count! (
      set /a count+=1
      set /a idx+=1
      echo.
   )
   set "line=%%A"
   call echo.%%line:%~1=%~2%%
   set /a count+=1
)

That will probably work for you. Might choke on special characters, though.

Ted

Post Reply