1

I am new to batch and this site. I searched for similar question and found some. But they are pointed towards having FART or other third party installations. But I need only batch script. Please help me in this.

I have multiple .txt files in a folder C:\files. All the files have text '>>superman<<' in them. I want to replace it with '>>batman<<' without creating any new files.

@echo off
SETLOCAL
for %%* in (.) do set foldername=%%~n*
SET stringtofindreplace=XXXX
for %%f in (*.fmw) do (
echo Processing %%f...
fOR /F "delims=" %%l IN (%%f) DO (
     SET "line=%%l"
     SETLOCAL ENABLEDELAYEDEXPANSION 
     set "x=!line:%stringtofindreplace%=%foldername%!" 
     echo(!x!
     ENDLOCAL)
)>%%~nf.new
)
GOTO:EOF

Thanks in advance

  • 2
    Possible duplicate of [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/) The command line tool xchange32 which just needs to be copied to same directory as batch file (no installation) can do that without the need of a batch file at all as it supports also wildcards for making the replaces on multiple files, i.e. `xchang32.exe /s *.fmw "search string" "replace string"`. – Mofi Dec 10 '15 at 08:38
  • 1. your description does not reflect the code snippet! 2. anyway, I think you need an additional pair of `()` around the `for /F` loop for the redirection `>%%~nf.new` for it to happen where you want; 3. you want to replace the original files, so just use `move /F "%%~dpnf.new" "%%~nxf"`; – aschipfl Dec 10 '15 at 09:59
  • Thanks for the help. I got the ans by using repl.bat from another similar thread – Kiran Prasad Dec 11 '15 at 11:02

0 Answers0