« Three weeks till year… | Home | Xojo 2023r4 released »

MBS Plugin Advent calendar: 12 - Files

Door 12 - Files

Fact of the day
Did you know that the first hard disk in 1956 only had a storage capacity of 3.75 megabytes? Today, many of the pictures we take with a high-resolution digital camera are already larger than that.

Today we come to a topic that I already mentioned in a previous door: Today we come to the Files section. This is all about your files. In Door 3, we already got to know the Files.FileExists function, which can tell us whether a file is stored behind a certain path. However, this function is not only available for files but also for folders. With Files.DirectoryExists you can check whether a path specified in the parameters leads you to a folder. If a file is hidden behind the path, the result is 0 and if it is a folder, the result is 1. If we want to know whether a folder or a file is hidden behind this path, we use the Files.ItemExists function, which returns a 1 for a file and a folder.

File Folder
Files.FileExists 1 0
Files.DirectoryExists 0 1
Files.ItemExists 1 1

If you have a file, you can use the component to query a lot of information about this file. Firstly, we have the Files.FileInfo function, which provides us with various pieces of information. This is intended for bundled app/plugin/framework files on Mac and on Windows for EXE/DLL files. In addition to the path, we also specify the desired selector in the function. On Mac, this can be Version, ShortVersion, MinimumSystemVersion, InfoString, ExecutableName and Identifier for the bundle identifier.

Under Windows, we have the selectors Description, Version, InternalName, CompanyName, LegalCopyright, OriginalFilename, ProductName, ProductVersion, and Copyright

We also have functions that query individual file information. For example: Files.FileName, Files.FileNameWithoutExtension, Files.AccessDate, Files.CreationDate, Files.ModificationDate, Files.FileKind, Files.FileSize, Files.IsHidden, Files.IsReadOnly, Files.IsExcludedFromBackup, Files.IsPackage, Files.GetFinderLabel or Icon.GetIcon. You can query all of this information using functions. For most functions, there is also a function with which you can change these properties.


You can also display a list of the files that are stored in a specific folder. This makes sense, for example, if you want to import all the files in a folder into the database. You can use Files.List for the list. If you would prefer to receive this list as JSON and get additional information about the files, you can use the Files.ListAsJSON function. If you use Files.List and Files.ListAsJSON, we always keep the list at the top level, which means we don't know what happens in the subfolders. For this we have the Files.ListRecursive function, which also returns the paths of the subfolders.

But you can not only query but also work with the files. You can create a new folder with Files.CreateDirectory. You can also copy one or more files from one folder to another using the Files.CopyFile and Files.CopyFiles functions. Creating an alias is also no problem with Files.CreateAlias.

Another cool thing that many of our customers use is that you can open a file from which you have the path, using a script. To do this, use the Files.LaunchFile function for normal files. This opens a file or folder. Use Files.Lauch e.g. to open a database file with FileMaker or your runtime solution. In addition, you can not only copy a file, but also move the file. This is where Files.MoveFile comes into play. A special form of moving is offered by the Files.MoveToTrash function, which places the file in the trash without a dialog. If you want to delete the file directly without going through the trash, you can use the Files.Delete function. In this case, use Files.DeleteFolder for a folder. There is another function for deleting that is still relatively new: Files.DeleteLater. This function writes a file to a list that is to be deleted afterwards. The files on this list are deleted when File Maker is closed. This function makes sense because there are some functions that can only be used with a path, such as the Files.LaunchFile function. So if we want to open a file from a container, we write this file to the temporary folder and enter the file in the deletion list.

Set Variable [ $Temp ; Value: MBS("Folders.UserTemporary") ] 
Set Variable [ $Name ; Value: MBS( "Container.GetName"; DoorTwelve::Container ) ] 
Set Variable [ $Path ; Value: MBS("Path.AddPathComponent"; $Temp; $Name) ] 
Set Variable [ $r ; Value: MBS( "Container.WriteFile"; DoorTwelve::Container; $Path ) ] 
Set Variable [ $r ; Value: MBS("Files.DeleteLater"; $Path) ] 
Set Variable [ $r ; Value:  MBS("Files.LaunchFile"; $Path) ] 

If we want to read a file from the disk into a container, this also works with this component. We have various functions for the different data formats that enable the file to be read: Files.ReadFile, Files.ReadJPEG, Files.ReadPNG and Files.ReadPDF.

Here you can see how a PNG is loaded into the container.

Set Variable [ $File ; Value: MBS( "Files.ReadPNG"; DoorTwelve::Path ) ] 
Set Field [ DoorTwelve::Container ; $File ]
 

I hope you enjoy using these functions, which I hope will also be helpful for you.


Monkeybread Software Logo with Monkey with Santa hat
11 👈 12 of 24 👉 13
12 12 23 - 12:58