computing
  • 28

Solved Batch File – Replace Text In File

  • 28

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

dword:00000001=”Banana”=dword:00000000=dword:00000001

Hope you can help me and many thanks already =)

message edited by CharlyT

Share

1 Answer

  1. 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
    		)
    	)
    )
    exit
    :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.

    • 0