Im trying to merge some txt files. Here is the logic of the rule:
- All .txt files are inside the same directory, progressively numbered.
- Files must be merged in blocks. Every block is delimited by a zero byte file.
So script will load all files between 2 zero bytes txt, including the first.
- Name of zero byte file must be put at the top of the newly aggregated.txt
and used to name it.
- as a plus, after merging the script should delete the source txt files (not the merged ones).
01.FileA (zero bytes)
The script will parse all files in a specified directory.
It will merge files 01 to 03 into FileA.txt (merged), 04 to 07 into FileB.txt (merged), etc.
so the name of the newly merged file will be the same of the zerobyte.
It will also put the filename (FileA) inside text, at the top of the merged file.
Thanks to this post I have the code to merge the files
but totally lack how to do the rest:
(* http://macscripter.net/viewtopic.php?id=12671 *) --scan the entire directory and stop when you find a 0kb file --merge this block of files together --name this resultFile with the name of the 0kb txt file. property resultFile : "this-should-be-a-variable-taken-from-the-0kb-txtfile.txt" property sourceFolder : "/Users/Shared/test/" set theFiles to (list folder (POSIX file sourceFolder) without invisibles) -- Empty the current results do shell script "echo '' > " & 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 --somewhere here grab the 0bk filename and write it at the top the merged file. end repeat --scan the next block of files or end
If a zero byte file isnt enough to delimit,
there can be a special string inside the file
(smt like $$$, instead of zero bytes).