Solved Batch File - Replace Text in File

September 1, 2015 at 02:54:21
Specs: Windows 7
Hey everyone,

I need help replacing "Banana"=dword:00000001" with "Banana"=dword:00000000"
inside a .txt

What I got so far is this

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE="C:\Temp\File.txt"
set OUTTEXTFILE="C:\Temp\File_out.txt"
set SEARCHTEXT="Banana"=dword:00000001
set REPLACETEXT="Banana"=dword:00000000

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

echo !modified! >> %OUTTEXTFILE%

The thing is that it gives the whole Output.
Only the replacement part is wrong.
Thats what it does in the line where it should replace the string


Hope you can help me and many thanks already =)

message edited by CharlyT

See More: Batch File - Replace Text in File

Report •

September 1, 2015 at 17:17:42
✔ Best Answer
Something like this?

@Echo off
setlocal enabledelayedexpansion
set in_file=file.txt
set out_file=outfile.txt
for /f "tokens=1,* delims=¶" %%A in (!in_file!) do (
	set string=%%A
	Echo !string! | findstr /l "Banana"
	if !errorlevel!==0 (
		Echo !string! | findstr /l "dword:00000001"
		if !errorlevel!==0 (
			call :modify
set modified=!string:1=0!
Echo !modified!>>!out_file!
goto :eof

If this doesn't help, consider posting the actual .txt file you want to parse data from.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

Report •

September 2, 2015 at 03:19:25
Hi RainBawZ,

Thanks for your help.
This worked perfectly fine for me.
I only had to Change one Little Thing

call :modify
)else (Echo !string!>>!out_file!)
) else (Echo !string!>>!out_file!)
This way it writes all the lines in the out_file.
I Need that to later on Change it into a .reg and Import it.
But I got it now.

So many thanks =D

Report •
Related Solutions

Ask Question