Computer Hope

Microsoft => Microsoft DOS => Topic started by: xabiy on October 06, 2008, 03:36:43 AM

Title: Batch file : replacing text part in files with FOR command
Post by: xabiy on October 06, 2008, 03:36:43 AM
Hello,

I have a TexRep software from No Nonsense Software. I can make my changes singly.
I'ld like to make my changes automatically. It means, there are several .htm files in a folder and need to change text parts in this files. How can I do it with FOR command in a batch file?
e.g:
texrep *.htm "texttofind" "replacewith"

Sorry if it is an resolved issue but I couldn't find.
Thanks,
Csaba
Title: Re: Batch file : replacing text part in files with FOR command
Post by: Sidewinder on October 06, 2008, 04:19:22 AM
Text files cannot be updated in place with batch code. This little snippet produces output files with a chg extension.

Code: [Select]
@echo off
setlocal enabledelayedexpansion

set /p search=Search String=
set /p replace=Replace With=

for %%v in (*.htm) do (
  for /f "tokens=*" %%a in (%%v) do (
    set newline=%%a
    set newline=!newline:%search%=%replace%!
    echo !newline! >> %%~nv.chg
)
)

If not run from the htm directory, you may have to add paths to the code.

Good luck.  8)
Title: Re: Batch file : replacing text part in files with FOR command
Post by: Dias de verano on October 06, 2008, 10:22:34 AM
Quote from: Sidewinder
Text files cannot be updated in place with batch code.

You can produce a new file which contains the changed text, delete the original file, then rename the new file with the original name.