Combine text files

I need to create a script that will join all of the text files in a specified folder into one new text file of a specific name. The folder might contain a single file or several files. Is this possible with AppleScript?

Will all these text files end with .txt?

Yes. They will all be *.txt.

However, the names will always be different and unpredictable. But they will always end in “txt”.

Put all of your files to be combined in a single folder (with the names in alphanumeric order indicating the order in which they should be combined), then try this:

set source_folder to (choose folder) as Unicode text
set the_files to (list folder (source_folder as alias) without invisibles)
repeat with i from 1 to (count the_files)
	do shell script "cat " & ¬
		(quoted form of (POSIX path of (source_folder & item i of the_files))) & ¬
		" >> " & ¬
		(quoted form of (POSIX path of (source_folder & "combined file.txt")))
end repeat

Jon

Model: PowerBook 1.25MHz
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

A modified version of Jon’s script:


property resultFile : "combined file.txt"

set sourceFolder to (choose folder)
set theFiles to (list folder sourceFolder without invisibles)
set sourceFolder to (POSIX path of sourceFolder)
-- Empty the current results
do shell script "echo -n '' > " & quoted form of (sourceFolder & resultFile)
repeat with currentItem in theFiles
	if name extension of (info for (POSIX file (sourceFolder & currentItem))) is "txt" then
		if ((currentItem as string) is not equal to resultFile) then
			do shell script "cat " & ¬
				(quoted form of (sourceFolder & currentItem)) & ¬
				" >> " & (quoted form of (sourceFolder & resultFile))
		end if
	end if
end repeat

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

This would be the simplified shell-script (just in case :wink: )

cat *.txt > output.txt

An alternative version (for jj :stuck_out_tongue: ):


property resultFile : quoted form of ("combined.txt")

choose folder
set source to quoted form of (POSIX path of result)
do shell script "cd " & source & "; rm -f " & resultFile & ¬
	"; cat *.txt > " & resultFile

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

How do I execute these shell-scripts? What application?

This is perfect! however, I don’t want the user to have to choose the “sourceFolder” each time. How do I indicate a specific folder that is always the chosen folder? (the .txt files change each time, but the folder will always remain the same location/name)

This is guardian34’s last post. I only proposed:
cat *.txt > output.txt
Instead of:
cat x.txt >> output.txt; cat y.txt >> output.txt; cat z.txt >> output.txt

Use one of these scripts. (You can change the properties at the top.)


property resultFile : "combined file.txt"
property sourceFolder : "/Users/Shared/Example/"

set theFiles to (list folder (POSIX file sourceFolder) without invisibles)
-- Empty the current results
do shell script "echo -n '' > " & quoted form of (sourceFolder & resultFile)
repeat with currentItem in theFiles
	if name extension of (info for (POSIX file (sourceFolder & currentItem))) is "txt" then
		if ((currentItem as string) is not equal to resultFile) then
			do shell script "cat " & ¬
				(quoted form of (sourceFolder & currentItem)) & ¬
				" >> " & (quoted form of (sourceFolder & resultFile))
		end if
	end if
end repeat


property resultFile : quoted form of ("combined.txt")
property source : quoted form of ("/Users/Shared/Example/")

do shell script "cd " & source & "; rm -f " & resultFile & ¬
	"; cat *.txt > " & resultFile

-->here's an old-fashioned version which doesn't use "do shell script"
-->assumes, like jonn8's script, that sourceFolder contains only .txt files
-->laziness on my part precludes error handling ;-)

property theSeparator : return & return & "= = = = = = = = = = = = = = = =" & return & return

set sourceFolder to (choose folder)
tell application "Finder" to set outputFolder to container of sourceFolder
set theseFiles to (list folder sourceFolder without invisibles)
set comboFile to open for access file ((outputFolder as string) & "combined.txt") with write permission
set startPoint to 0.0
repeat with thisFile in theseFiles
	set thisData to read file ((sourceFolder as string) & thisFile)
	write thisData & theSeparator to comboFile starting at (startPoint + 1)
	set startPoint to get eof comboFile
end repeat
close access comboFile