For each and search and replacing lines of text in file

I have a text file with a collection of lines, such as:

2016_097 - C - passport - Covering certificate.wpd
2016_097 - C2 - passport - Covering certificate.wpd
2016_097 - H - passport - Covering certificate.wpd
2016_097 - M1 - passport - Covering certificate.wpd

I wish to replace 'Covering certificate.wpd' 'complete document'

I have tried to use the For Each action but trying various options I cannot get it work, including:

Help would greatly appreciate to indicate what I am doing wrong.

You are copying each line to the clipboard and then replacing the strings on the clipboard. (Not in the file.) That’s why you don’t see changes in your file.

You can replace without clipboard:

  1. Read the file to a variable. (Usually no need to read it line by line.)
  2. Do the Replace on the contents of the variable.
  3. Write the variable back to the file (or to a different file).

Edit:

Here is an example, in its basic form:

Replace Text in File.kmmacros (2.4 KB)

2 Likes

Tom,

Thank you pointing out where I was going wrong and for suggesting an alternative and better solution.

Given the paucity (ie complete absence as far as I am aware) practical examples of how For Each The Lines in works, I would be grateful from someone knowledgable in its use on how this option works and what use it might have.

Basically it goes like this:

  • The action itinerates through all the lines of a given text file / variable / clipboard.
  • The current line is written to the “For each” variable; vtest in your macro.
  • With the sub-actions you add under “execute the following actions” you can do something on each itineration. Using the vtest variable in the sub-actions you can access the content of the current line.

Some use-cases:

  • You have a file/variable where each line is the path to an app or file. With the appropriate action you could for example open, read, delete, etc. each app/file.
  • You want to extract certain lines from a file/variable.
  • You want to count lines.
  • …

Given the paucity (ie complete absence as far as I am aware) practical examples of how For Each The Lines in works

Well, there is…

3 Likes