Read Multiple .txt Files and Append to a Combined .txt File

I save OCR'd pages to a designated folder as .txt files. The filenames are [date time] so that they stay in page order.

I am trying to achieve the following:

  • Read contents of each .txt file in order
  • Copy contents of each file to a clipboard/clipboards
  • Append all the clipboards in order
  • Write a single file containing all the text combined

Example (with file contents)

040422 1000.txt > even a stopped clock
040422 1001.txt > gives the right time
040422 1002.txt > twice a day

Desired output in a single .txt file: even a stopped clock gives the right time twice a day

There may be a different number of .txt files in the folder depending on the number of pages I've OCR'd, which is what makes it harder for me to set up.

Any help would greatly be appreciated.

Thank you!

Here's one approach, reading, in this example, from ~/sampleFolder (where ~ represents your home path) and writing out the combined contents of the sorted files to to ~/Desktop/combined.txt

Text files sorted by name and combined.kmmacros (5.7 KB)

2 Likes

PS there's discussion here of various options and pitfalls for doing this with a shell command line:

shell - Concatenating multiple text files into a single file in Bash - Stack Overflow

but FWIW my personal experience is that in practice the Keyboard Maestro interface, or a typed scripting language, wastes less time on each task of this kind, and costs less up-front time to learn.

2 Likes

ComplexPoint, thank you so much for your time - this macro works perfectly!

1 Like