Capture File Name When Importing

Each week, I import a csv invoice file with a different file name. After importing, I’m manually entering the file name and saving it as a $variable. Is there a way to capture or trap the file name automatically? I’ve Googled a bit and found some references to plugins that may help, but I’d like to do this natively, if possible. (I do have Base Elements installed, however.)

Hi @steverichter ,

If this were an un-hosted solution, then one option would be to rejigger how the import happens as follows:

  • Instead of going directly to an Import Records script step where the user selects the import file, you would start with an Insert File step, with a global container field as the target. (The Insert File script step would still prompt the user to select the file.)
  • Once the file has been imported into the global container field, you can use the GetContainerAttribute function to allow your FMP code to determine the file name.
  • Then, you would have code that would either export (or write via v.18 File script steps) the file to some location such as the temp directory.
  • And then, once the file is written to the temp directory, you can have a scripted Import Records step perform the import, and this can be without a prompt for a file location, because your code will already know precisely where the file exists.

Some comments:

  • Definitely more moving parts and pieces, but not so many more that I would rule it out -- just enough to require me to admit that has what I refer to as a higher "Rube Goldberg index".
  • One nice variation is that, instead of a prompt for the user to select the file, this could be set up as a script trigger on the global container field, as presented in the layout. On MacOS, this makes for a nice drag-and-drop UI for importing the file. I can't say one way or the other whether it works, or is as elegant, on Windows.

Regarding the mention of this working for an un-hosted file:

  • It also would work on a hosted solution, but there is an extra caveat, which is that inserting the file into the global container field may cost some extra overhead. Contrary to what I would have imagined, I have been informed that the act of inserting a file into a global container field results in some network activity when the solution file is hosted. Not what I would expect for a global field, but several folks whom I consider reliable have told me this is so.
  • A variation that I've sometimes used for a hosted file is to drag and drop the file to a non-global container field of a record that is uniquely reserved for that user, and then have a PSOS script retrieve the uploaded file from the same record, and then the PSOS script exports that file (server-side), and performs the import. If PSOS no-wait is used, this can have the nice effect of the user not having to wait for the import to complete (depending on what their next steps are, of course).

In all cases you'd have more work cut out for you with respect to error trapping all the things that could go wrong with all the various moving parts. This is where just sticking to Import Records certainly has its KISS appeal. That said, I've used the variations mentioned above enough times to appreciate what they have to offer -- but they did take much more scripting effort to set up.

HTH -- It may be more info than what you were looking for, but hopefully it still feels relevant.

2 Likes

It’s hosted; however, I’ll investigate the info you posted. I appreciate your help.

1 Like

Looks as though, if you are willing to leverage the installed BE plugin, that this would be a pretty easy task. I wasn't sure if BE had a file selection function, but it appears that it does.

In which case:

  • Use BE to prompt the user to select a file
  • BE returns the path to the file
  • Your code can use the path to script the import
  • Your code can also parse the file name from the path

One of the more recent updates to FMP includes some very useful path-conversion functions. I don't know if these would be necessary for this task, but if you found that BE was returning the path in a format that did not seem to work out of the box, then I'd give the path conversion functions a try.

1 Like

hi @steverichter

If the CSV you import each week is always the same format, then set up your import record step as you usually would, but change the path of the import to be a variable, e.g. $path.

Next, as part of your import script, you can obtain the file name and path by inserting it into a container field as a reference. By doing so you are prompting the user to select the file, and also obtaining the filename.

The container will contain 2 lines of information:

file:[[filename]]
[[filepath]]

You can use Getvalue to obtain the second line which is the path to the file.

You can also extract out the filename, OR you can just use GetContainerAttribute function to obtain the filename

Hope this makes sense & helps you out.

2 Likes

Taking it a step further, in the Insert File script step, set the Storage Options to Reference and set the Target to Variable. No need for a plugin or global field since that variable will have both the file name and path. Use that info to import for real.

1 Like