Page 1 of 1

How best to replace exact text in a given file?

Posted: 13 Jul 2015 10:55
by mmahon512
Hi,

I have a template file that has the following abbreviated text in it.

Code: Select all


@echo off
REM - -------------------------------------------------------------------------
REM - File Name:    --FileName
... (abreviated)
@echo on



I created a command file to copy this template file and give it a new name. Here is the contents of that command file.

Code: Select all


@echo off
set file_name=c:\scripts\cmds\cmd-%1.cmd
cp c:\scripts\cmds\_cmd.template %file_name%
REM sed 's/--FileName/%file_name%/g'<%file_name% >%file_name%
@echo on



I tried using sed but I would prefer to learn the dos way of replacing just the text '--FileName' copied from the template in the new file. I found an executable fart.exe but I think it does batches of files and not just a single file. Any help is greatly appreciated. Thanks for your time and consideration.

- Mike

Re: How best to replace exact text in a given file?

Posted: 13 Jul 2015 11:06
by foxidrive
You will find two batch files here on Dostips that can manipulate text.

They are both native Windows batch scripts and are very powerful tools,
having Sed-like capabilities and Grep-like features.


Findrepl.bat by Aacini
viewtopic.php?f=3&t=4697

Jrepl.bat by Dave Benham
viewtopic.php?f=3&t=6044


If you need help to use them then tell us what you are manipulating and how you need it changed.

Re: How best to replace exact text in a given file?

Posted: 10 May 2020 04:28
by mmahon512
i love this site. Thanks ifoxdrive :shock: