Modify a .txt file with a batch file (without a text editor)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

Modify a .txt file with a batch file (without a text editor)

#1 Post by Mado91 » 18 Mar 2019 11:42

Hi programmers,
here's my today goal: I would really be amused to learn how to edit a .txt file just using batch comands without using a text editor.
Easy things just like the following steps:
1) open file.txt
2) replace all the letters "a" with the letter "b"
3) save file.txt
4) close file.txt
Thanks everyone!

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Modify a .txt file with a batch file (without a text editor)

#2 Post by Squashman » 18 Mar 2019 12:07

There is no three and four in your example. All writing is done with redirecting Standard output to a file using the > symbol.

Since Windows does not come with a stream editor you will need to use a FOR /F command to read the file and the SET command to do string substitution.

Do a little research on those two commands and let us know what you get stuck on.

Mado91
Posts: 13
Joined: 16 Mar 2019 08:03

Re: Modify a .txt file with a batch file (without a text editor)

#3 Post by Mado91 » 18 Mar 2019 14:59

Hi, thanks a lot for the precious answer. I searched infos about FOR and SET comands, still I'm not sure how it can be helpfull for edit a .txt :cry:
some practical example would be very usefull to me, but I haven't found one, just more ore less theorical explanatioms I'm not "ready" to understand properly.

For example, if I have a file.txt containing the text "Hello world", what's the entire bath code that allows me to change the text into "Hello;world" or "Goodbye world" or anything else?

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Modify a .txt file with a batch file (without a text editor)

#4 Post by Squashman » 18 Mar 2019 15:33

How about you start with learning how to read a file using the FOR /F command.

Then learn how to change the contents of a variable with the set command.

Both of these commands have extensive help file documentation.

Once you have mastered those two techniques individually you should be able to understand how to accomplish your task.

Most people learn to crawl before they walk or even run. You are trying to run without learning the first two basic steps.

Post Reply