Replace content of txt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchrookie_99
Posts: 3
Joined: 30 Dec 2014 00:07

Replace content of txt

#1 Post by batchrookie_99 » 30 Dec 2014 00:39

Hi everyone,

Just start learning batch command and can't figure out how to do this.

My question :
One folder has many .txt files and I want to replace their content.

001_Family.txt
002_Family.txt
003_Family.txt
...

The content likes below:
HelloWorld
HelloFriends
123456
...

I want to change "World" to "Family".
I knew how to read HelloFriend string and assign variable but don't know what to do next.

For %%A IN (*.*) Do Call :Check "%%A"
:Check
Set File=%~nx1
Set Change=%File:~4,6%
find "Hello%Change%" %File% > nul
???

Much appreciated :D

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

Re: Replace content of txt

#2 Post by foxidrive » 30 Dec 2014 00:49

batchrookie_99 wrote:My question :
One folder has many .txt files and I want to replace their content.

The content likes below:
HelloWorld
HelloFriends
123456
...

I want to change "World" to "Family".


Do you just want to solve the task? Test this on copies of your files.

Code: Select all

@echo off
for %%a in (*.txt) do call jrepl "HelloWorld" "HelloFamily" /L /F "%%a" /O -


This uses a helper batch file called `Jrepl.bat` (by dbenham) - download from: https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat

Place `Jrepl.bat` in the same folder as the batch file or in a folder that is on the path.

batchrookie_99
Posts: 3
Joined: 30 Dec 2014 00:07

Re: Replace content of txt

#3 Post by batchrookie_99 » 30 Dec 2014 19:24

Hi Foxidrive

Thanks for your help. :D
I downloaded the bat file and tried it.

It indeed changed the output result in command windows but I need the txt file content also be replaced.
Sorry for my unclear information.

For example,

The content of abc.txt
HelloWorld
HelloFriend

After executed XX.cmd

The content of abc.txt will be
HelloFamily
HelloFriend

Does this bat file has any function can do it? I barely understand these codes. :cry:

batchrookie_99
Posts: 3
Joined: 30 Dec 2014 00:07

Re: Replace content of txt

#4 Post by batchrookie_99 » 30 Dec 2014 19:29

I just found I can use

for %%a in (*.txt) do call jrepl "HelloWorld" "HelloFamily" /L /F "%%a" /O > result.txt

I have no problem now, :D :D

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

Re: Replace content of txt

#5 Post by foxidrive » 30 Dec 2014 20:34

batchrookie_99 wrote:It indeed changed the output result in command windows but I need the txt file content also be replaced.
Sorry for my unclear information.


You were clear, and my suggestion works fine here to change the files.

Make sure that the - character is at the end of the line.

Post Reply