Search & Replace - I'm getting crazy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
six1zero
Posts: 7
Joined: 07 Apr 2010 08:54

Search & Replace - I'm getting crazy

#1 Post by six1zero » 07 Apr 2010 09:02

Hi,
probably it's a stupid thing but I don't understand what is wrong.

I tried to use BatchSubstitute.bat, but doesn't work on my machine.

C:\>BatchSubstitute.bat Andrea AAA ReinsuranceValues.txt
find: invalid predicate `'
The process tried to write to a nonexistent pipe.

I tried to change the predicate ' with ´ or ` but the error is:

C:\>BatchSubstitute.bat Andrea AAA Values.txt
The system cannot find the file ┤"type Values.txt|find /n /v """┤.

C:\>BatchSubstitute.bat Andrea AAA Values.txt
The system cannot find the file `"type Values.txt|find /n /v """`.

Which is the secrects to use it?

Thanks in advance
Andrea

six1zero
Posts: 7
Joined: 07 Apr 2010 08:54

Re: Search & Replace - I'm getting crazy

#2 Post by six1zero » 07 Apr 2010 10:18

without single quote '

C:\>BatchSubstitute.bat Andrea AAA Values.txt
"%line%"

C:\>

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

Re: Search & Replace - I'm getting crazy

#3 Post by ghostmachine4 » 08 Apr 2010 04:27

i recommend you to try out sed. download from : http://gnuwin32.sourceforge.net/packages/sed.htm

Code: Select all

c:\test> sed -i.bak "s/Andrea/AAA/g" file.txt


-i.bak is an option to save your file first before sed makes changes to your file.

six1zero
Posts: 7
Joined: 07 Apr 2010 08:54

Re: Search & Replace - I'm getting crazy

#4 Post by six1zero » 08 Apr 2010 09:05

Thanks for your suggestion.

I resolve the problem removing the find after pipe, more slow but works.
Following the code to resolve my problem, now the batch file don't need the parameters and works automatically with multiple file in a particulary directory.

Thanks to dostips!

@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 [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$source http://www.dostips.com

SET ELS_DRIVE=%ELSHOMEDIRECTORY%
SET SPOOLTV=%ELS_DRIVE%\SpoolTV
SET FILE_PREFIX=ReinsuranceValues
SET FILE_SUFFIX=.ps

echo %ELS_DRIVE%
%ELS_DRIVE%
cd %SPOOLTV%

REM -- OldStr [in] - string to be replaced
SET OldStr=P:

REM -- NewStr [in] - string to replace with
SET NewStr=

echo %FILE_PREFIX%*%FILE_SUFFIX%

for %%i in (%FILE_PREFIX%*%FILE_SUFFIX%) do CALL :MIO %%i

:MIO
SET myTempFile=%1
IF "%myTempFile%"=="" GOTO:EOF
echo.
echo %myTempFile%

REM -- Get first part of file name until "." (dot) and the second part after "." (dot)
for /f "tokens=1,2 delims=. " %%a in ("%myTempFile%") do set myNameFile=%%a&set myExtensionFile=%%b

REM -- Rename the original file (ex. FileName_orig.extension)
set myFile=%myNameFile%.%myExtensionFile%.orig
ren %myTempFile% %myFile%


if "%OldStr%"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims==" %%A in ('"type %myFile%"') do (
set "line=%%A"
if defined line (
call set "line=echo.%%line:%OldStr%=%NewStr%%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X>>%myTempFile%
) ELSE echo.
)

srikarmrao
Posts: 1
Joined: 04 May 2010 16:30

Re: Search & Replace - I'm getting crazy

#5 Post by srikarmrao » 04 May 2010 16:56

If working on windows servers, you can use change.com pathofthefile %oldstr% %newstr%

change.com is a windows utilities tool. free for windows servers we can download from microsoft website.

Post Reply