how can i replace string to other string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

how can i replace string to other string

#1 Post by r2du-soft » 04 Aug 2012 14:09

hi
how can i replace string to other string
in text file
for example this code replace a word in text file:


@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
rename "test.txt" "test.tmp"
for /f %%a in (test.tmp) do (
set foo=%%a
if !foo!==reza set foo=reeza
echo !foo! >> test.txt)
del test.tmp





but i need a code for replace string to other string!
for example:

replace:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\

Instead:

HKEY_LOCAL_MACHINE\SOFTWARE\


how can?What code? :?:

thanks


r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

Re: how can i replace string to other string

#3 Post by r2du-soft » 05 Aug 2012 03:13

thanks Squashman
BUT IM NOT FOUND CODE!!!
looking this image:

Image

im want When open bat file"REPLASE TO TEXT.bat"
replase:
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\
Instead:
HKEY_LOCAL_MACHINE\SOFTWARE\
in the text file "test.txt"
:?:
thanks

Can someone please help \ Can someone please help \ Can someone please help
Last edited by r2du-soft on 05 Aug 2012 04:15, edited 1 time in total.

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

Re: how can i replace string to other string

#4 Post by foxidrive » 05 Aug 2012 04:14

There are examples at that link which work.

Here is another method:

Call this batch file below SAR.BAT and launch it like this:

call SAR.BAT "REPLASE TO TEXT.bat" "test.txt" "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\" "HKEY_LOCAL_MACHINE\SOFTWARE\"


Code: Select all

::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs

r2du-soft
Posts: 68
Joined: 09 Sep 2011 12:13

Re: how can i replace string to other string

#5 Post by r2du-soft » 05 Aug 2012 04:29

thanks
for replace pen to book What should be replace this code?
thanks

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

Re: how can i replace string to other string

#6 Post by foxidrive » 05 Aug 2012 05:01

r2du-soft wrote:thanks
for replace pen to book What should be replace this code?
thanks


call SAR.BAT "REPLASE TO TEXT.bat" "test.txt" "pen" "book"

Mark Heim
Posts: 2
Joined: 05 Aug 2012 08:52

Re: how can i replace string to other string

#7 Post by Mark Heim » 05 Aug 2012 10:41

Code: Select all

@echo off
echo reza  > test.txt
copy test.txt  test2.txt
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f %%i in (test.txt) do (
echo %%i >> test.txt
set foo=%%i
if !foo!==reza set foo=reeza
echo !foo! >> test.txt)
echo type test.txt
echo.
type test.txt
echo.

sed "s/reza/reeza/g"  test2.txt > test3.txt

echo type test3.txt
echo.
type test3.txt


[EDIT aGerman] Corrected the code tags [/EDIT]
Last edited by Mark Heim on 05 Aug 2012 10:49, edited 2 times in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how can i replace string to other string

#8 Post by Squashman » 05 Aug 2012 14:26

r2du-soft wrote:thanks Squashman
BUT IM NOT FOUND CODE!!!

Maybe I am misunderstanding what you are trying to do but I am pretty sure the 2nd link I posted is what you are trying to do. The Batch Substitute code replaces text within a file.

Post Reply