How do I compile 700 text docs into 1?

Hi Guru’s
I have 700 .txt files in a folder containing information that I want to compile into one document, open, select all, copy, paste to a single doc - save, a simple macro?
HA! First time I’ve seen Applescript and I thought it would be quicker than doing it by hand after doing the first 100 - so here I am 4 hours later with my script opening TextEdit and the files and nothing more!! Totally bemused and about to return to Cmd A,C,W,V,S 600 more times!

Can anyone tell me how?

Please no flames for my stupidity or laziness!

Well another 100 files done by hand
Is it possible to use Applescript to do this?
Can someone tell me that?
Thanks :o)

Yes, it’s possible and, once the script is written, it will likely only take seconds or minutes to handle the task (unless the files are extremely large). It would probably be best to use a capable text editor such as BBEdit (which, by the way, is recordable). There’s probably a way to do it quite quickly in the shell but I don’t know much about the shell.

– Rob

Well at least my initial solution wasn’t too far out then thanks for the confirmation!

I’ll look into BBEdit too just in case it can be accomplished in less than two more days!

javascript:emoticon(‘:lol:’)

I don’t know if TextEdit can handle a large document but it shouldn’t be too difficult to write a script. Are all of the files in one folder? If so, is that all that’s in the folder? What is the average size of the text files?

– Rob

Maybe this will be sufficient. It just handled 200 small text files in a few seconds.

set output_ to (choose file name with prompt "Choose a name and location for the new file.")
set folder_ to (choose folder with prompt "Select the folder containing the text files.")
tell application "Finder" to set files_ to files of folder_ as alias list

try
	set file_ref to open for access output_ with write permission
	repeat with file_ in files_
		set text_ to read file_
		write (text_ & return) to file_ref starting at eof
	end repeat
	try
		close access file_ref
	end try
on error e
	display dialog e
	try
		close access file_ref
	end try
end try

– Rob

My God! - just tested your script - it works as planned!!!
20 seconds replaces 2 more days, the word count is over 250,000. Happily I don’t have to ever open the file in TextEdit so waiting a few minutes for it to open wont be a problem!

I hoped to find a reason to understand Applescript and on the strength of this I may read a bit more about it with the time you’ve saved me!

Once again thanks - my fingers and brain were getting very tired :slight_smile:

Glad to help.

– Rob

Hi,

I’m impressed by your script Rob, I’ve been wanting to do something similar for a while but I don’t know anything about Applescript. Maybe you can help me out too. I have a bunch of documents in Comma Separated Variable (CSV) format that I’d like to combine into a single Excel file. Your script works to put them one after the other but I need them side by side. Is there a way to do that? I’m guessing it’s a lot more complex than I think…

Thanks,

discobiscuit

Hi discobiscuit,

Thanks for your kind words. I’m sorry to say that I have absolutely no scripting experience with Excel and I don’t even possess it. I wonder if substituting a comma for the return would accomplish what you want. Maybe this will work.

set output_ to (choose file name with prompt "Choose a name and location for the new file.")
set folder_ to (choose folder with prompt "Select the folder containing the text files.")
tell application "Finder" to set files_ to files of folder_ as alias list

try
	set file_ref to open for access output_ with write permission
	repeat with file_ in files_
		set text_ to read file_
		write (text_ & ",") to file_ref starting at eof
	end repeat
	try
		close access file_ref
	end try
on error e
	display dialog e
	try
		close access file_ref
	end try
end try

If this doesn’t work, you might be able to get accurate advice from someone else by starting a new topic which refers to Excel. Good luck!

– Rob

Thanks for the reply and the script Rob. Unfortunately that doesn’t seem to do the job, but thanks for trying. I suspect that it’ll actually end up being a complex task since the file format is not ‘pure’ CSV, but instead has some info at the start and end (that I don’t actually need) and I think it has a few hard returns at the top to makes things that little bit more tricky.

Anyway, I’ll start a new topic as you suggest, thanks again for your help.

discobiscuit