Getting a long string of text into a text file

I’m making an archive application that zips giant project folders and drops an accompanying text file next to it with the text of all the file names in said folder. I then add that text as metadata in our metadata manager (which has a 65,000 character limit). I was taking that text variable and then dumping it into a text file via the echo terminal command. In practice it’s been giving me frequent errors like

I’m guessing this is something to do with illegal file names in the project files being a problem in Terminal. Is there a way around this? I tried textedit but I was having a tough time getting the file to save. It would also be nice to keep things in terminal. Thanks for your help! Donated to macscripter today! It’s been a lifesaver

on open droppedItems
	tell application "Finder"
		set archivePath to "/Volumes/SAN/Post_Transcoding/CustomTranscoding/VirtualAE_DO_NOT_DELETE/ArchiveZip"
		set folderList to every item of droppedItems as list
		repeat with i from 1 to count of folderList
			set theFolder to item i of folderList
			set theFolderColonPath to POSIX file (do shell script "dirname " & quoted form of POSIX path of (theFolder)) as string
			set theFolderPath to POSIX path of theFolderColonPath
			set AppleScript's text item delimiters to space
			set file_list to name of every file of entire contents of theFolder as text
			set spaced_list to file_list as string
			set AppleScript's text item delimiters to ""
			if (count spaced_list) > 65000 then
				display dialog "You have too many characters of metadata, please break up into smaller zip files."
				return
			end if
			do shell script "cd " & theFolderPath & " && zip -r -3 " & (archivePath) & "/" & (name of theFolder as text) & ".zip" & " " & (name of theFolder as text)
			do shell script "echo '" & file_list & "' >" & archivePath & "/" & (name of theFolder as text) & ".txt"
		end repeat
	end tell
end open

Hi.

Where possible, you should use ‘quoted form of’ to quote text values and paths in shell script text. This not only puts a single quote at each end of them, but makes sure that characters like apostrophes are properly handled too.

Note that, as written, your script only includes the names of visible files in the text file, whereas the archive will contain folders and invisibles too. You may want to include a check to make sure that each dropped item actually is a folder before trying to list its files.

on open droppedItems
	set archivePath to "/Volumes/SAN/Post_Transcoding/CustomTranscoding/VirtualAE_DO_NOT_DELETE/ArchiveZip"
	set folderList to droppedItems
	repeat with i from 1 to (count folderList)
		set theFolder to item i of folderList
		set AppleScript's text item delimiters to space -- linefeed would make the text easier to read.
		tell application "Finder"
			set file_list to name of every file of entire contents of theFolder as text
			set theFolderName to name of theFolder
		end tell
		set AppleScript's text item delimiters to ""
		if (count file_list) > 65000 then
			display dialog "You have too many characters of metadata, please break up into smaller zip files."
			return
		end if
		do shell script ("zip -r -3 " & quoted form of (archivePath & "/" & theFolderName & ".zip") & " " & quoted form of POSIX path of theFolder)
		do shell script ("echo " & quoted form of file_list & " >" & quoted form of (archivePath & "/" & theFolderName & ".txt"))
		-- Or the two shell scripts could be combined:
		-- do shell script ("zip -r -3 " & quoted form of (archivePath & "/" & theFolderName & ".zip") & " " & quoted form of POSIX path of theFolder & " ; echo " & quoted form of file_list & " >" & quoted form of (archivePath & "/" & theFolderName & ".txt"))
	end repeat
end open