Merge duplicate text files

I’ve started using SublimeText to take meeting notes instead of Drafts. The main reason being that I prefer the interface, I want to access the files on any device at any time (using OneDrive), and to save notes as text files rather than within another application.

This post isn’t to discuss the merits of the workflow (I’m only just trying it so I don’t have a strong opinion yet), but what I would really like to do is to merge meeting notes with the same name.

Say I meet John every week, and I create a text file called John.txt in a folder called Inbox. I’d like Hazel or some other service to scan my OneDrive folder, find a matching name, and then merge my file to the exisiting file in its current location (e.g. in the Meetings folder).

Is this possible on a Mac?

A few thoughts:

  1. Yes, I think what you describe is certainly possible. But it would involve some significant scripting. (Hazel can’t do it on its own—it would need to trigger a script of one sort or another that does what you want.)

  2. In my experience, Hazel and OneDrive don’t play well together. Hazel changes things quickly enough that OneDrive’s sync gets confused and ends up creating multiple copies of files. Of course, you may have better luck than I have.

  3. If you really just intend to append/prepend text to an existing file, why not just open that file when it’s time to take new notes? You could use Spotlight or a smart folder based on tags or something similar to make those note folders easy to find quickly.

Finding files with a certain name is easy. Appending the contents of the new text file to the existing file is also easy. The hard part is part is accounting for human fallibility. What if you have two people named John? What if you forget or get confused about your file naming convention? What if you have two files named John because you decided you needed a copy in another folder? Should both files be changed or only one? If only one, which one?

Think about these and the other edge cases you might encounter. They will determine how complicated the script needs to be. Let’s assume that there is only one file called John.txt in the Meetings folder to which the contents of the John.txt file in the Inbox should be appended. Then the script to do you what you want is as follows:

cat "~/OneDrive/Inbox/John.txt" >> "~/OneDrive/Meetings/John.txt"

You can generalize this script to append the contents of all text files in the Inbox folder to the text file with the same name in Meetings folder and create the file in the Meetings folder if it does not exist.

for i in $( ls "~/OneDrive/Inbox/"*.txt )  # loop through all the text files in the Inbox
  do outfile="~/OneDrive/Meetings/"`basename "$i"`  # construct the path for the Meetings file that corresponds to a text file in the Inbox
  echo "\n" >>  "$outfile"  # goes to next line of Meetings file
  date  >> "$outfile"  # prints the current date and time
  cat "$i" >> "$outfile" # appends the Inbox file contents to the Meetings file
  echo "\n" >>  "$outfile" # goes to next line in Meetings file
  rm "$i" # Deletes the file in the Inbox folder so you don't add the same thing twice.

Hopefully this gives you enough of a framework to make what you need.

1 Like