Newbie Q: Sort and concatenate text files
Newbie Q: Sort and concatenate text files
- Subject: Newbie Q: Sort and concatenate text files
- From: John Whalen <email@hidden>
- Date: Sun, 27 Apr 2003 09:19:03 -0700 (PDT)
Dear Applescripters,
I was able to cobble together an applescript which
takes a text files as input, sorts them by name, and
concatenates them to a single file (see below).
Is there a way to sort the files by creation date
rather than name?
[Note, I based much of the process on two sample
applescripts from the apple web site]
Thanks for your assistance!
on open fileList
set my_list to fileList
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
set this_data to return & "START:" & space &
((current date) as string) & return & return & "Files
merged in the following order:" & return & return
set this_file to (((path to desktop folder) as text)
& "MERGE LOG FILE")
my write_to_file(this_data, this_file, true)
try
repeat with i from 1 to (number of items in
index_list)
set this_item to item i of index_list as text
set currentFile to item this_item of fileList --as
text
set this_data to (i & tab & currentFile as text) &
space & "STATUS OK" & return
set this_file to (((path to desktop folder) as
text) & "MERGE LOG FILE")
my write_to_file(this_data, this_file, true)
set the_content to read currentFile
--header for file
set updated_content to "cond" & tab & "seg"
repeat with c from 1 to 129
set updated_content to updated_content & tab &
"ch" & c
end repeat
set updated_content to updated_content & return
--completed header
repeat with j from 1 to ((number of paragraphs in
the_content) - 2)
set updated_content to updated_content & i & tab &
j & tab & paragraph j of the_content & return
end repeat
-- last line gets no return to avoid spaces
set updated_content to updated_content & i & tab &
((number of paragraphs in the_content) - 1) & tab &
paragraph ((number of paragraphs in the_content) - 1)
of the_content
set this_data to updated_content
set this_file to (((path to desktop folder) as
text) & "MERGE.txt")
my write_to_file(this_data, this_file, true)
end repeat
on error error_message number error_number
set this_error to "Error: " & error_number & ". " &
,
error_message & return
set the log_file to ((path to desktop) as text) &
"MERGE ERROR LOG"
my write_to_file(this_error, log_file, true)
end try
set this_data to return & "END:" & space & ((current
date) as string) & return & return
set this_file to (((path to desktop folder) as text)
& "MERGE LOG FILE")
my write_to_file(this_data, this_file, true)
end open
--next to scripts taken from Apple web site
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ,
open for access file target_file with write
permission
if append_data is false then ,
set eof of the open_target_file to 0
write this_data to the open_target_file starting at
eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
on ASCII_Sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.