How to load a Text file and change its content?

I have a need to process template file which will contain a few place holders like for example <<>>.

I have never had to do this type of work so I don’t know as to how to load a user selected text file and replace a few place holders with user entered text.

I want to read the original file, make changes to its content and then save it to a temp file in the application folder under a different name.

TIA

  1. Use folderitem.ShowOpenFileDialog() to get the user-selected file
  2. Open it as a textinputstream and use ReadAll to read it into a string
  3. Use string.IndexOf or a regex to find your placeholders. Note that if the placeholder can also occur elsewhere, e.g. inside a string or comment, you may need more sophisticated parsing to correctly identify the placeholders.
  4. Find the beginning and end of each placeholder, and then update the string something like:

`myString = mystring.left(beginning) + newContent + myString.middle(end)

Note that you’ll need to be careful about including the placeholder delimiters, as well as keep in mind that middle() and index() are now zero-based – watch out for off-by-one errors.

Work through the source string, repeating for all placeholders.

  1. Save the string back out by creating the new file as a textoutputstream and writing the string to it.
1 Like