Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Replacing text in a .txt file with other text?

Author
Message
Shadowtroid
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location: nope
Posted: 22nd Jun 2010 03:30
I am not very good at programming (planning to get good at it when I can wrap my head around it better) however I do wish to use small bits for helping me do work. Is there any way to open a .txt file, find a string of text anywhere in it, and replace it with another string of text?

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 22nd Jun 2010 06:29 Edited at: 22nd Jun 2010 06:57
Okay, this is something that I literally threw together in a hurry. It is not real pretty, but should demonstrate one way you can accomplish this.

In this example, I suggest searching for the word "indeed" (without the quotes) and replacing it with "nevertheless" (without the quotes). The text will print the old version and new version. If for whatever reason, it does not find the search string, it will tell you.



I have attached the text file to be used with this.

Hope this helps you,

LB

EDIT: It is worth mentioning that this code converts the text file to separate strings and subsequent searches can be done by simply looking at the string array. (i.e. Keep an original copy of the text file if you want to run it again.)


Attachments

Login to view attachments
Shadowtroid
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location: nope
Posted: 22nd Jun 2010 13:32
Wow, thank you. I have absolutely no idea how that works.

Well, I guess I'll be blankly staring at this for a while now.

Thanks again, LBFN.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 22nd Jun 2010 15:40
Basically, it opens the text file so that it can read it, which it does byte by byte. It stores the bytes into a temporary string until it sees a byte value of 32, which is a space. It then saves the temporary string into a dimensioned string array for use later.

The code goes through the entire file and breaks it down so that every individual word is saved as a separate string. Once this is done, it checks against the target string and replaces it. Then it re-writes the file using the strings. It is possible to change the code to write the file as bytes, like it originally was.

Hope this helps.

LB


Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Jun 2010 17:14 Edited at: 23rd Jun 2010 18:01
It's a lot easier to deal with one line at a time rather than put each word in an array. Using IanMs Matrix 1 Utilities makes it easier to replace text too.



You can get IanMs Matrix 1 Utilties here.

https://forumfiles.thegamecreators.com/download/1972494

Attachments

Login to view attachments
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 23rd Jun 2010 17:20 Edited at: 23rd Jun 2010 17:26
Here's a simpler way to do it, but you'll need IanM's Matrix1Utils Plugin. That plugin is pretty much a must have for any DBPro user anyway, since it provides so many useful commands that DBPro doesn't have.

Anyway, here's the example. The only reason you need the plugin is for the Replace All$ command, which replaces any text in a string with some other text. It's heavily commented, but let me know if there's something you don't understand .



I have attached a text file for use, but feel free to make your own. The code will open the text file (it's called "TestFile.txt"), replace "Hello" with "Goodbye", and then save it as "TestFileReplaced.txt".

EDIT: Looks like Grog beat me to it . Both examples do pretty much the same thing, only his doesn't need arrays.

Attachments

Login to view attachments
Shadowtroid
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location: nope
Posted: 23rd Jun 2010 18:07
Wow guys, thanks. I'll be keeping all of these code snippets for future use.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Jul 2010 15:02
I'd also suggest that if you have large files or a lot of files to carry out the text replacement on that you switch to my file handling too - it's just far faster than the built-in stuff.

Login to post a reply

Server time is: 2024-11-24 14:58:20
Your offset time is: 2024-11-24 14:58:20