MBS FileMaker Advent calendar - Door 9 - Archives
Day 9 - Archives |
Our monkey likes to have things together in a compact form. For this reason, it also likes archives and would like to have the option of creating an archive from the children's information and the individual files in the portal. We want to implement this now.
We can create various archives with the MBS plugin. The following formats are available: 7zip, ar, arbsd, argnu, arsvr4, bsdtar, cd9660, cpio, gnutar, iso, iso9660, mtree, mtree-classic, newc, odc, oldtar, pax, paxr, posix, raw, rpax, shar, shardump, ustar, v7tar, v7, warc, xar und zip.
Probably the best known is the zip format. We also want to use this today. To do this, we first create a new archive with Archive.Create. In the parameters, we first specify zip as the format and then have to decide on a filter. Depending on the individual formats, there are various filters that can be specified. For the zip format, you can choose between "store" or "deflate", whereby "deflate" provides loss-free data compression on the files and "store" does not apply any compression. Optionally, we can also specify the desired storage location for the zip file. If this is on the hard disk, we simply enter the path here, otherwise we leave this parameter empty. The other two parameters are further options, such as encryption and a password for the archive. However, we do not need these today.
We want to compress our zip archive with deflate and save it to the desktop. This means we create a path. To do this, we use Path.AddPathComponent again and this time the Folders.UserDesktop function to determine the path to the desktop.
Set Variable [ $Path ; Value: MBS("Path.AddPathComponent"; MBS("Folders.UserDesktop"); Giftee::first_name & "_" & Giftee::last_name & ".zip") ] Set Variable [ $Archive ; Value: MBS("Archive.Create"; "zip"; "deflate"; $Path) ]
As long as the archive is open, we can add files. We now want to do this with our portal files. To do this, we first go to our portal with Go to Object and enter the name of our portal here. Then we go through the portal entry by entry in a loop. To do this, we first get the file into a variable and then write this file into the archive using Archive.AddContainer. In the parameters of this function, we specify the file first and then the file name that the file should have in the archive.
Go to Object [ Object Name: "Files" ] Go to Portal Row [ Select: Off ; First ] Loop [ Flush: Always ] Set Variable [ $Data ; Value: Files::File ] Set Variable [ $r ; Value: MBS("Archive.AddContainer"; $Data; Files::File_Name) ] Go to Portal Row [ Select: Off ; Next ; Exit after last: On ] End Loop
If you want to write a file from the disk to an archive, use the Archive.AddFile function and first enter one or a list of file names that are to be transferred from a specific folder to the archive. Then enter the path to this folder.
Now we not only want to transfer the files to a recipient, but also their personal data. We write these in a text file. To do this, we first put together the text that we want to have in this file and run through the two portals for telephone and e-mail in the same way as shown above with the files portal.
Set Variable [ $text ; Value: "ID: " & Giftee::PrimaryKey & "¶Name: " & Giftee::first_name & " " & Giftee::last_name & "¶Birthday: " & Giftee::Birthday & "¶Address:¶" & Giftee::Street & "¶" & Giftee::Postcode & " " & Giftee::City & "¶" & Giftee::State & " " & Giftee::Country & "¶¶" ] Set Variable [ $Text ; Value: $text & "¶¶Telephone:" ] Go to Object [ Object Name: "Telephoneportal" ] Go to Portal Row [ Select: Off ; First ] Loop [ Flush: Always ] Set Variable [ $PhoneTyp ; Value: Telephone::Type ] Set Variable [ $PhoneNumber ; Value: Telephone::Number ] If [ $PhoneNumber ≠ "" ] Set Variable [ $Text ; Value: $text & "¶"& $PhoneTyp & ": " & $PhoneNumber ] End If Go to Portal Row [ Select: Off ; Next ; Exit after last: On ] End Loop # Set Variable [ $Text ; Value: $text & "¶¶Email:" ] Go to Object [ Object Name: "EmaiPortal" ] Go to Portal Row [ Select: Off ; First ] Loop [ Flush: Always ] Set Variable [ $MailTyp ; Value: Email::Typ ] Set Variable [ $MailAddress ; Value: Email::Emailaddress ] If [ $MailAddress ≠ "" ] Set Variable [ $Text ; Value: $text & "¶"& $MailTyp & ": " & $MailAddress ] End If Go to Portal Row [ Select: Off ; Next ; Exit after last: On ] End Loop
We then write this composite text to the archive as a text file using Archive.AddText, specifying the text, the desired encoding and the file name.
Set Variable [ $r ; Value: MBS("Archive.AddText"; $Text; "UTF-8"; Giftee::first_name & "_" & Giftee::last_name & "_Data.txt") ]
Because we now have all the data we want in our archive, we close it again with Archive.Close. If we had decided at the beginning that we wanted to have the archive returned as a container value instead of saving it in a file, we could pass the name of the file as a parameter here.
Set Variable [ $r ; Value: MBS("Archive.Close") ]
Now we can try out our script and voila it creates an archive with the desired files.
I hope to see you again tomorrow for the 10th door.
8 👈 | 9 of 24 | 👉 10 |