I've always wanted to be able to create a directory or folder on the local hard drive from a FileMaker file. Products such as the Troi File Plug-in (www.troi.com) are able to easily perform this task but it's also nice to know what your FileMaker Pro-only options are, because they don't require licensing a third-party product.
The big roadblock to storing a folder in FileMaker Pro is the limitations of a Container field. Container fields can only store files. The workaround is to compress an empty folder into a .ZIP or .SIT file. You can then insert this file into a Container field using the File... menu item from the Insert menu. Make sure to insert the .ZIP or .SIT file with the "store only a reference to the file" not selected.
After you have the folder inserted into a global Container field, you can create the following script:
Set Variable [$Path; Value: "file:" & Get(FileMakerPath) & MYTABLE::xfolder_container]
Export Field Contents [MYTABLE::xfolder_container; "$Path"; Automatically open]
Pause/Resume Script [Duration (seconds): 2]
Commit Records/Requests [Skip data entry validation; No dialog]
Export Field Contents ["$Path"]
The Set Variable script step specifies a local variable as the path for the following Export Field Contents script steps. The formula uses the Get(FileMakerPath) function to place the zipped or stuffed folder into the FileMaker Application folder but you can use any path to specify a path on the local hard drive:
Get(DesktopPath)
Get(DocumentsPath)
Get(FilePath)
Get(PreferencesPath)
Get(FileMakerPath)
Get(TemporaryPath)
Make sure to precede the path returned by the functions above with "file:" to complete the path requirements for a file. The name of the path is provided by the name of the compressed folder stored in the global Container field. When you store files in a Container field without selecting the reference option, FileMaker Pro returns the name of the file as the text result of a calculation formula. If you use a referenced file in a calculation, it returns the path to the file. Be careful when using the Get(FilePath) function because it returns the path to the file on the server if the file is being hosted.
The first Export Field Contents script step exports the contents of the global Container field to the path specified in the variable. The option to automatically open the exported file is selected so the .ZIP or .SIT file is decompressed. Before you run this script, you must make sure to set up .ZIP and .SIT files to open with Stuffit Expander so they automatically decompress without any dialogs. You can do so on a Macintosh using Get Info on any .ZIP or .SIT file and setting the "Open With" option to Stuffit Expander. Make sure to also click on Change All to apply the settings to all .SIT and .ZIP files. I don't believe Windows has a corresponding option to decompress files without dialogs so this technique is Macintosh-only.
At this point, the script pauses for two seconds so the folder has time to decompress. After the pause, the script commits the record so no fields are selected. This step is important for the last Export Field Contents script step which exports without a target field. If a field happens to be selected, the contents of that field would be exported. In this case, no target field should be specified or selected for the .ZIP or .SIT file to be deleted.
This basic version of the script works great but doesn't track whether the folder exists already. To prevent the existing folder and its contents from being deleted, you must add some scripting code:
Set Error Capture [On]
Set Variable [$Path; Value: "file:" & Get(FileMakerPath) & Left(GetAsText(MYTABLE::xfolder); Length(GetAsText(MYTABLE::xfolder_container)) - 4) & "/"]
Import Records [No dialog; $Path; Picture and movie files]
If [Get(LastError) = 100]
#Place existing script code here!
Else
Show Custom Dialog ["Folder Already Exists"; "The folder already exists in the FileMaker Application directory."]
End If
My good friend Jeff Cohen showed me this technique and we worked together to streamline the error-checking process. The problem was how to check for an existing folder with the same name. Jeff had already come up with the idea of placing a text file in the folder. If the file existed, you knew the folder existed. I started thinking, "what feature in FileMaker Pro works with folders so I don't have to use a file to check whether the folder exists?" The feature I came up with was importing the contents of a folder.
To make this technique work, first turn on Set Error Capture. The next step is to set the $Path variable to the location of the folder that might (or might not) exist. You do so by removing the last four characters from the end of the .ZIP or .SIT file stored in the global Container field. With the path set correctly, the script attempts to import the contents of the folder. If the folder doesn't exist, an error 100 is returned.