Naming Files through Scripting in FileMaker

Subscribe to Our Blog

Choosing the Name

From exporting data to excel sheets to custom print layouts, automated naming for exported files can help with organization and create a more professional look.




The first thing to consider when scripting to automate file naming would be to determine a field that makes the record or set of records unique. For single records, it will most likely be a user facing identification field such as, Job Number, Serial Number, or perhaps Username and Job Title as long as it’s unique. For scripts that print a set of multiple records, you’ll typically name the file based off what makes that group unique- maybe Region or State, or often the date or timestamp the export was performed.

Naming using a Field

As I mentioned earlier, in order to prevent FileMaker from attempting to save multiple files as the same name, you will need to include a field with unique data in the filename. Sometimes this will be easy and natural. For example, if you’re printing single Work Order records and they have a field such as Work Order Number that is defined to have user friendly unique data, you will be safe to save files to a single folder and will be able to have a direct correlation to Files and records.

Since no two records can have the same Inspection Number, the only overwriting that can take place would be over an older version of the same record.

If there is no visually pleasant identification field (Work Order Number, Job Number, Case Number…), OR you want the ability to save historical/multiple files per single record; then you will probably want to include a date or even timestamp with a not necessarily unique field such as last name or company name. Example: ContactInfo_RyanMcTeer_8_18_2020.xlsx

Be careful though, several characters inside of a timestamp will cause errors if you try to save them within the file name. Getting the filename to print as it did in the example will take some scripting.

Naming using a Date or Range

The characters inside of a timestamp will cause errors are colons, spaces, and slashes. Make sure you use a Substitute function to remove those characters, as I’ve shown in the data viewer below.

Using a timestamp in the file name may seem unduly lengthy, but it does guarantee unique file names, and will be easy to organize and navigate through.

Also, sometimes the date or time range is part of the data. Perhaps you have a reporting script that asks the user for a time range, then prints or exports all the records that fit that criteria. In a situation like that, you would want the date range in the file name. Shown below is an example of how I would do that, using just dates instead of timestamps.

Locations Provided by FileMaker

Sometimes clients will specify the location they want exported files saved to. When they don’t specify, I typically export to the Temporary Path, and select the setting to automatically open the file. That way the user will be reminded to save the file wherever they want, every time a file is exported.

An example of saving a PDF to the temporary path is shown below:

In this example, the $FileName variable must end with the “.pdf” extension, but we will get to File Naming later. Other options as far as export locations would involve replacing the “Get (TemporaryPath)” inside the $filePath variable.

FileMaker provides the option of using the built-in function, “Get (DesktopPath)” which is obviously just as fast and easy, but most users are not going to want to clutter their desktop by saving files to it regularly.

Custom Locations

Other paths will need to be manually constructed, which you can learn about in the FM Help Documentation. It is important to notice, when manually constructing file paths, the path will be different across different operating systems, and your script will need to check the results of Get (SystemPlatform) and construct the path accordingly. This option takes significantly more time and effort than using the two FileMaker provided paths (desktop & temporary).

One common location that clients ask for that is not provided by FileMaker would be the Downloads folder. To save time and effort, I reuse a custom function:

Let([
docpath = Get ( DocumentsPath ) ;
c1 = If ( Right ( docpath ; 1 ) = "/" ; 1 ; 0 ) ;
slashcount = PatternCount ( docpath ; "/" ) ;
adj1 = If ( c1 = 1 ; Position ( docpath ; "/" ; 1 ; slashcount - 1 ) ; Position ( docpath ; "/" ; 1 ; slashcount ) ) ;
trim = Left ( docpath ; adj1 - 1 )
];
Case (
c1 = 1 ; trim & "/Downloads/" ;
c1 = 0 ; trim & "/Downloads" ;
"0"
)
)


What’s nice about using this custom function is it will point to the Downloads folder on both Mac OS X and Windows, and you can refer to it just as easily as the TemporaryPath or DesktopPath. Instead of setting the $filePath variable to Get (TemporaryPath), you can set it as “DownloadsFolder” or whatever you name the custom function.

Using these techniques, you should be able to spend a bit more extra time on your printing and exporting scripts, but gain all the benefits of files saving to the destination you want, with the file names you want.