I'd like to combine multiple text files, all ending in .txt, into the same document with the filename of each separating the contents of each. I've found ways to combine the contents, I've found ways to combine the filenames, but the closest I've gotten to doing both is the below superuser question, which is exactly what I want, but the comments suggesting how to use the code on MacOS doesn't seem to work, likely because the question is almost a decade old. I'm on mac OS Big Sur 11.2.3. I'd appreciate any help!
combine multiple text files, _+ filenames_, into a single text file
EDIT: At the advice of the bot, I've copied and pasted the clarifying details from the previous question posted, as it asked it very succinctly. Here is an example of what I am looking for
* a filename
contents of file
...
* another filename
contents of file
...
etc...
EDIT: Further details
I'm running it through Automator, feeding the files in through 'Get Specified Finder Items' and "Get Folder Contents" that then feeds "Run Shell Script" with this script copied below. It says everything runs good, giving 'workflow completed' and no errors, but I don't have 'target-file.org' anywhere.
find -regex '.*\.\(docx?\|org\|rtf\|te?xt\)$' | while read file
do
echo "* $file" >> target-file.org
cat "$file" | pandoc -t org >> target-file.org
done
EDIT: Solution, if not what I'd started looking for
I realized I was overcomplicating this by trying to do it all in one step. I found some other stuff to run through automator's power shell that allowed me to add a file name to the top of each document, then when that was done combine all text with the very easy to use cat command. Code that is currently working for me.
cd /Filepath_to_my_folder
for file in *.txt
do
ed -s $file < <(printf '%s\n' 1i "$file" . wq)
done
cat *.txt > Merged.txt
find. Make also sure pandoc is installed, else remove that. part as the answer suggested. – Destroy666 May 19 '23 at 19:24