7 Replies Latest reply on Mar 29, 2013 2:01 PM by JohnEriksson

    Get file name and file size from a container field



      Get file name and file size from a container field


      Hi!  New to this forum but not to FMP, altho just now using v.11.  Hoping someone can help with a problem that has me stumped.


      Developing an app with starter Document Library as basis, but instead of typing in a file name then using the Insert File button, I want to use Insert Object to insert a linked PDF file in the File container(1 per record), which I can then dbl-click to bring up the original file if I wish.  No problem there - works fine.


      But then I want to have the File Name (and File Size) fields automatically display that info based on the PDF in the container.  I have tried both a (new) script for the Insert File button and a field def for File Name as a calculated field, both using GetLayoutObjectAttribute(File,"content"), which should return a text data type but either is not working or is returning an empty string.  (I also added GetAsText function later to the same calc, just to be sure.)   Same thing happens when I substitute "objectType" or "bounds", etc for the attribute name.  I know the container insert is working OK because the PDF appears there and the link to the original file works fine.  Still no joy if I embed the inserted PDF instead of linking, or if I insert as a file or picture instead of as an object.  No matter what I do the other fields I want to automatically fill in with the file name (or path) and file size remain empty.  Like I say -- I'm stumped!


      Can anyone help?  Many thanks!

        • 1. Re: Get file name and file size from a container field

          This should do the trick (returns the name of the attached file within a container field) . . .


          Create a calculation field with


          GetField ( "Attachment" )


          Note: the container field name [in the above example the field is called 'Attachment'] needs to be within quote marks

          • 2. Re: Get file name and file size from a container field

            With a file Inserted [x] As referencey only, GetAsText ( container field ) (no quotes needed or wanted) returns 2 lines, the relative and absolute paths. The file name can be calculated from either line, but my calculations use the last (which also works for Insert Picture).


            Let ( [

            Info = GetAsText (Contain);

            vc = ValueCount (Info);

            path = GetValue ( Info; vc );

            pat = PatternCount ( path; "/" );

            pos = Position ( path; "/"; 1; pat );

            len = Length (path)



            Right ( path; len - pos )




            Alternatively, you can also use the Position() function, in which case the Let() is 1 line shorter. 


            Let ( [

            Info = GetAsText (Contain);

            vc = ValueCount (Info);

            path = GetValue ( Info; vc );

            len = Length (path);

            pos = Position ( path; "/"; len; -1 )



            Right ( path; len - pos )




            Alternatively, you can use GetField(), as Edudata suggested. If you have FileMaker 10+, a better way to get the field name, without having to hard code it within quotes, is to use the GetFieldName() function (in Logical functions section). Then it will not break if you change the field name later.


            GetField ( GetFieldName ( Contain ) )



            I don't know of any native FileMaker function which can return the actual size of a referenced file. The Troi File plug-in can return it, via an unstored calculation, but it is rather expensive, unless you need more cross-platform file manipulation.


            On a Mac a simple AppleScript call can return the size (according to the OS, which changed in Snow Leopard, to use 1000 instead of 1024). That would require a calculation to coerce the FileMaker syntax absolute file path to an old style Mac path, which is fairly simple.


            But its main limitation (besides being Mac only) is that you'd have to run it via a script step. It is quite fast however. 


            Once you have a Mac file path, the basic AppleScript to get the size would look like:

            set file_bytes to size of (info for file aFile)


            You would also need to set it into a FileMaker field in the AppleScript, but I don't know your fields, so cannot post that. It would look like:

            set cell "file size field" of current record to file_bytes

            "file size field" would need to be on the current layout, with this simple construction.

            • 3. Re: Get file name and file size from a container field

              Thanks much!  It does indeed work.


              I've also found that this works: Set Field [Document Library::FilePath;Substitute (GetValue(Document Library::File;2);"file";"")] -- where FilePath is the text field to receive the document path, and File is the container name.


              But for either to work, you have to use Insert File to load an icon of the file into the container field (named "File" in the example above).  Insert Object will load the actual PDF document, but then the above examples do not work (return an empty string or a ?).  Not a big deal, since user can double-clk on the icon to open the document in Adobe Acrobat, but I was hoping to both be able to show the document in the container and get the file name.


              So far, even loading document with Insert File, I haven't been able to retrieve the document size.

              • 4. Re: Get file name and file size from a container field

                That's what I was saying, that there is no native FileMaker function to get the file size. But AppleScript can easily get it, as well as Windows command line (somehow), as well as a few free plug-ins. MooPlug for example (Windows only, but you can fork the script), or 360Works' ScriptMaster (cross-platform, you write code via a varient of Java; but there's examples; to get file size: 

                new File(filePath).length()


                I do not know if MooPlug returns it dynamically* (too lazy to boot up Windows to see :-). If so, then that's the way to go on Windows (though you'd need to run an AppleScript to do it on a Mac).* Troi File plug-in can return it dynamically also, but it costs $$; however it is very useful if you do a lot of cross-platform file manipulation, including text editing within files.



                I think the way I would approach this would be control the Insert File operation from the start. You can use either AppleScript on a Mac and/or MooPlug to "choose file", returning its path to FileMaker. You can then use that, coerced to FileMaker syntax, set into a script Variable, then Insert File using that as the path.


                Somewhere during this operation you can get what you want from the file, forking the script for Mac/Windows operations (or using ScriptMasters Java, if you can find/write the code you need).


                *By "dynamically" I mean an unstored calculation updating itself whenever it becomes visiable. Troi File does this, and fairly quickly; can be used in a Find.

                • 5. Re: Get file name and file size from a container field

                  Thanks, Fenton.


                  After reading your posts I think I'll give MooPlug a try -- I was not aware of this plugin until now.  Right now have my hands full with another FM project, but as soon as I can get around to it I'll follow your suggestions.


                  Again - many thanks!


                  • 6. Re: Get file name and file size from a container field

                    um.... no native function in filemaker...

                    ------------------ BEGIN QUOTE -------------------------

                    Which insert option are you using to insert the file?

                    Store by reference stores the entire filepath plus additional data in the field. If you want to see all the text, define a calculation field set to return text and just enter the name of your container field as its calculation, then put this field next to your container field. You'll then see exactly what text is entered each time you insert a file or picture.

                    In a test file where I used insert file, I get this text:

                    filewin:/C:/Documents and Settings/Phil/My Documents/My Pictures/RaceCars/Bentley-Le-Mans-Racer.jpg

                    GetValue ( ContainerFIeld ; 2 ) will return the second line which is the complete filepath.

                    When I use Insert Picture, I get:

                    image:../../My Documents/My Pictures/RaceCars/Bentley-Le-Mans-Racer.jpg
                    imagewin:/C:/Documents and Settings/Phil/My Documents/My Pictures/RaceCars/Bentley-Le-Mans-Racer.jpg

                    And thus I use 3 instead of 2 to get the third line of text.

                    ---------------------- END QUOTE --------------------------


                    • 7. Re: Get file name and file size from a container field

                           Getting filesize is easy. Length( Containerfield ) will return the size in bytes. An own function can recalc it to kB/MB.

                           Ah, the question was about referenced file. no idea. :)