How best to replace exact text in a given file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mmahon512
Posts: 2
Joined: 13 Jul 2015 10:42

How best to replace exact text in a given file?

#1 Post by mmahon512 » 13 Jul 2015 10:55

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

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

#2 Post by foxidrive » 13 Jul 2015 11:06

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.

mmahon512
Posts: 2
Joined: 13 Jul 2015 10:42

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

#3 Post by mmahon512 » 10 May 2020 04:28

i love this site. Thanks ifoxdrive :shock:

Post Reply