Find and replace multiple words in files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cloud_DE
Posts: 1
Joined: 05 Jul 2019 04:47

Find and replace multiple words in files

#1 Post by cloud_DE » 05 Jul 2019 06:09

Hi All,

I have been searching and trying to modify the replace string script but looks like it is printing extra echo.with every line, can any one please suggest how i can search multiple string and replace with new word

here is the script I am using :

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
::          OldStr1 [in] - string to be replaced
::          NewStr1 [in] - string to replace with
::          OldStr2 [in] - string to be replaced
::          NewStr2 [in] - string to replace with
::          File   [in] - file to be parsed

for /f "tokens=1,* delims=]" %%A in ('"type %5|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        call set "line=echo.%%line:%~3=%~4%%"
        for /f "delims=" %%g in ('"echo."%%line%%""') do %%~g
		
		
    ) ELSE echo.
)
)
it is updating the first and second string (new_user,new_pwd) with new strings but getting "echo." in beginning of each line .

output:

echo.create user <new_user> with <new_pwd>;
echo.commit;


Appreciate your help.Thanks!

penpen
Expert
Posts: 1685
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find and replace multiple words in files

#2 Post by penpen » 08 Jul 2019 10:53

If you only want to get rid of the "echo"'s in the output, then you just could use:

Code: Select all

@echo off
cls
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
::          OldStr1 [in] - string to be replaced
::          NewStr1 [in] - string to replace with
::          OldStr2 [in] - string to be replaced
::          NewStr2 [in] - string to replace with
::          File   [in] - file to be parsed


for /f "tokens=1,* delims=]" %%A in ('"type %5|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=%%line:%~1=%~2%%"
        call set "line=%%line:%~3=%~4%%"
	call echo("%%line%%"
    ) ELSE echo.
)
)
Sidenotes:
Is there any reason not to use deleayed expansion?
Your script neither contains any "echo.create user..." nor "echo.commit" parts... is it missing i your script?
If you only wnat to create user accounts... why do you need to replace strings anyway?


penpen

Post Reply