Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file : replacing text part in files with FOR command  (Read 2792 times)

0 Members and 1 Guest are viewing this topic.

xabiy

  • Guest
Batch file : replacing text part in files with FOR command
« 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

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Batch file : replacing text part in files with FOR command
« Reply #1 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)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Dias de verano

  • Guest
Re: Batch file : replacing text part in files with FOR command
« Reply #2 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.