# Batch file to replace words in a text file



## lunarlander (Sep 22, 2007)

Hi,
I am trying to write a batch file that replaces a word in a text file with another word given as a batch parameter. I know of the FOR /F command to loop thru the lines. I know also about doing a simple string replacement. But I can't seem to piece it together. Can somebody help me?


----------



## askey127 (Dec 22, 2006)

lunar,
take a look at Fixedit and the user guide in my sig.
It was created for these kinds of issues.
You may not need a batch file at all.


----------



## Squashman (Apr 4, 2003)

Give this a try.
http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace


----------



## lunarlander (Sep 22, 2007)

Hi Squashman,

I tried out the dostips.com bat file. Can't get it to work. Plus, I can't understand it. 

Hi Askey

Your program is an exe correct? Can't use that as my users will have to install a separate program.


----------



## Squashman (Apr 4, 2003)

lunarlander said:


> Hi Squashman,
> 
> I tried out the dostips.com bat file. Can't get it to work. Plus, I can't understand it.


Well I know it works. I am a moderator on DosTips and we don't put stuff in the function library that do not work.

I can't help you unless you provide more information. You will have to provide examples of what you tried that did not work.


----------



## lunarlander (Sep 22, 2007)

Hi Squashman,

I named the batch file replace7.bat and called it using: 
replace7.bat bonobo juko takeo.txt

The takeo.txt contains:
this is a test bonobo thing
this is a test

The output only shows 'echo off'.


----------



## Squashman (Apr 4, 2003)

lunarlander said:


> Hi Squashman,
> 
> I named the batch file replace7.bat and called it using:
> replace7.bat bonobo juko takeo.txt
> ...


Works just fine on my computer.

```
C:\Batch\FindReplace>replace7.bat bonobo juko takeo.txt
this is a test juko thing
this is a test
```


----------



## lunarlander (Sep 22, 2007)

Found the problem. I was missing the last closing bracket when I copied and pasted the bat file. Thanks for helping out.

One more question. Where do I add a 4th parameter for use as the output file? I know it is the lines with echo, but when I add %4 the batch file breaks. I also tried 'replace7 bonobo juko takeo.txt > outfile.txt' , but the 'echo off' is shown also in the outfile.txt .


----------



## Squashman (Apr 4, 2003)

lunarlander said:


> Found the problem. I was missing the last closing bracket when I copied and pasted the bat file. Thanks for helping out.
> 
> One more question. Where do I add a 4th parameter for use as the output file? I know it is the lines with echo, but when I add %4 the batch file breaks. I also tried 'replace7 bonobo juko takeo.txt > outfile.txt' , but the 'echo off' is shown also in the outfile.txt .


So you can't use redirection like the examples show in the link?


----------



## lunarlander (Sep 22, 2007)

But the problem with the first example given is that the output shows the 'echo off', not just the output.
And I can't get the second example to work.


----------



## Squashman (Apr 4, 2003)

lunarlander said:


> But the problem with the first example given is that the output shows the 'echo off', not just the output.
> And I can't get the second example to work.


Again, the examples provided in the link work for me as they are shown using your data for input.


----------



## lunarlander (Sep 22, 2007)

OK Thanks again, Squashman. I missed out the first character '@' in the first line. This time I increased the screen font size and saw it. And I copied and pasted on a Windows machine.


----------



## Squashman (Apr 4, 2003)

There is a little link to the right of the code that says: *Download: BatchSubstitute.bat*.


----------



## lunarlander (Sep 22, 2007)

Thanks Squashman, I see it now. Would have saved some work.


----------



## lunarlander (Sep 22, 2007)

I have automated an entire Bank department writing an accounting back end system , but I just don't get batch files. I also tend to avoid technology that is designed for 64MB RAM and IBM XT equipment.


----------



## Squashman (Apr 4, 2003)

lunarlander said:


> I also tend to avoid technology that is designed for 64MB RAM and IBM XT equipment.


CMD.exe (previously command.com) has evolved a lot since the days of DOS. Same goes for other shell scripting languages like BASH on nix systems.

More functionality was added to it with Windows 10.


----------

