Page 1 of 1

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

Posted: 18 Mar 2019 11:42
by Mado91
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!

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

Posted: 18 Mar 2019 12:07
by Squashman
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.

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

Posted: 18 Mar 2019 14:59
by Mado91
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?

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

Posted: 18 Mar 2019 15:33
by Squashman
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.