Saturday, March 24, 2018

#1 2017-09-10 06:35:54 am

Registered: 2008-03-21
Posts: 198

Create new files from UTF8 plain Text splitting with paragraph CR

I need to import in a File Maker Pro db the content of various files.
each file contains from 1 to 50 CR separated lines they all follow the very same structure.
I include one line as such where each line  does not include spaces or any other symbol.

Filename 0052.txt
content of the file
520101 <tab> 52 <tab> 01 <tab> 01 <tab> 00:00:36:03 <tab> _DSC9439.MOV <tab> 19/7/2017 09:44:28 <tab> /Volumes/SHOWDOW_2017/OriginalClips <tab> B2A34529CFA6F17F
520102 <tab> 52 <tab> 01 <tab> 02 <tab> 00:00:36:12 <tab> _DSC9440.MOV <tab> 19/7/2017 09:46:34 <tab> /Volumes/SHOWDOW_2017/OriginalClips <tab> 8E1F97C4A1425D2C
520202 <tab> 52 <tab> 02 <tab> 00 <tab> 00:00:44:04 <tab> _DSC9441.MOV <tab> 19/7/2017 09:52:38 <tab> /Volumes/SHOWDOW_2017/OriginalClips <tab> 3D1002D1DD046171
520201 <tab> 52 <tab> 02 <tab> 01 <tab> 00:01:02:10 <tab> _DSC9442.MOV <tab> 19/7/2017 09:54:30 <tab> /Volumes/SHOWDOW_2017/OriginalClips <tab> 00148EABAEA7F886

The Script should name the new text file with the first word in each paragraph as such
and include each paragraph.

Then move to the next file where the structure is exactly the same, until every file in the folder is done

I have more than 2000 files and doing it manually is practically imossible
Thanks and regards

Browser: Safari 537.36
Operating System: Mac OS X (10.10.5)



#2 2017-09-10 04:12:12 pm

Registered: 2010-02-19
Posts: 113

Re: Create new files from UTF8 plain Text splitting with paragraph CR

The following script will read tab separated files from Filemaker.  Note that Filemaker does not pad a space before and after the tab as the data you provide does.  So, I did not account for any spaces before or after the tab in this solution.

To use: First, select the file(s) you wish to convert.  You can pick multiple files in the choose file dialog by holding down the command key and selecting the files.  Second, select the folder where you want the converted file to be saved.


set tabFiles to (choose file with prompt "Select tab file..." of type {"txt"} with multiple selections allowed)
set saveFolder to (choose folder with prompt "Select folder to save results to:") as text

repeat with aFile in tabFiles
   set tabFile to aFile as text
   set tabRecords to read file tabFile using delimiter {return}
   set theData to tabRecords as text
   set fileName to (word 1 of first item of tabRecords) & ".txt" as text
   set theFile to saveFolder & fileName as text
       set dataOut to open for access file theFile with write permission
       set eof of dataOut to 0
       write theData to dataOut starting at eof
       close access dataOut
   on error
           close access file theFile
       end try
   end try
end repeat

on getItems(theRecord)
   copy the text item delimiters to origDelims
   set the text item delimiters to tab
   set recordItems to {}
   set recordItems to every text item of theRecord
   set the text item delimiters to origDelims
   return recordItems
end getItems

Browser: Firefox 50.0
Operating System: Mac OS X (10.10)



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)