Creating a Folder from FileMaker (Windows)
Being that many of the contributors on the forum that I frequent are Mac-centric, I thought I would share a simple way to create folders on a windows machine. I will give you the code, and then break down what it does, so you can tweak it to fit your needs.
Script Step: Send Event
Text: cmd /c md C:/FolderName
Action: Creates folder(s)
cmd – this is what tells Windows what you want to do. In this case, open the Command line app.
/c – tells the cmd line to carry out the following string and then stop (in this case, “md FolderName”)
md – simply…Make Directory. That is the lingo for a folder.
FolderName – the folder name (note if you are going to use spaces in the folder name you need to enclose the whole path in Double Quotes.
This command will create a folder with the following path: C:/FolderName
Creating a Folder in a Folder
If you want to create a folder inside another folder, just ad the path you want. For example, to create a folder on my desktop in a folder called FM Stuff I would use a path similar to this (notice the use of quotes):
cmd /c md “C:/Documents and Settings/username/Desktop/FM Stuff”
Creating a Folder Based on Field Data
That is all well and dandy, but Josh I want to create a folder using some data I have in FileMaker…how do I do THAT, huh, HOW DO I DO THAT???
“Simple”, I would say ::smug little grin::
Select the “Calculation” option in the “Send Event” Options dialog and enter something like:
“cmd /c md ” & Quote ( Table1::FolderName )
This would create a folder to the path specified in that field (assuming you have the full path). Sweet, right? Now, obviously you need to substitute the actual table name and field names, but you get the idea.
Examples
To create a folder on your desktop using the FolderName field (you need to trim off the first / that appears before the path using the Get (DesktopPath) function). I have included a second option here for creating a folder on the current user’s desktop. Because the default root in the command line interface is the current user’s folder, any action (unless specified otherwise) works on the context of that current user’s folder.
“cmd /c md ” & Quote ( Right (Get ( DesktopPath ); Length (Get (DesktopPath))-1) & TableName::FieldName )
or
“cmd /c md ” & TableName::FieldName
To create Folder1 and Folder2 (inside of Folder1) on the desktop:
“cmd /c md ” & Quote ( Right (Get ( DesktopPath ); Length (Get (DesktopPath))-1) & “Folder1\Folder2″)
To create a folder using the data in TableName::FieldName at a specified location:
“cmd /c md ” & Quote ( “C:/Documents and Settings/username/Desktop/Music Files/” & TableName::FieldName )
You’re the bomb! This works like a charm. Thank you very much.
June 22, 2011 at 2:40 PM
Worked very nicely on FM9. Through some experimentation, I discovered that multiple folders can be created in a single Send Event by separating each folder path with a comma. I.E. “cmd /c md ” & Quote ( “C:\Folder 1, C:\Folder 2″ )
The same can be achieved by repeating the Send Event code, but each Send Event will briefly open its own CMD window – ugly if you’re creating 10 folders.
The only trouble I ran in to was the use of the backslash (\). I switched to the forward slash(/) and everything worked in Win XP.
June 30, 2011 at 12:54 PM
Hi, I am using Filemaker Pro 11 and was wondering if you could tell me how to create the new directory reletive to the location of my database file.
For example, say I have a database file located in “C:\Databases\database.fp7″ and would like to create the directory “C:\Databases\Invoices” but IF the location of my database was “C:\CompanyName\Databases\Database.fp7″ then I would like the script to create the directory “C:\CompanyName\Databases\Invoices”.
This should work no matter where my database file is located.
Cheers!
January 12, 2012 at 8:10 PM
Ben – appreciate the heads up on the slashes. It was correct in my test database, I must have typed it wrong.
January 12, 2012 at 11:52 PM
Hey John,
I haven’t tested this code, but it should work. You can replace “YOUR_NEW_FOLDER” (including the quotes) with your new folder, if any. You can also replace it with a field if you want to create the folder from a value in a field in the record. Pay careful attention to the use of quotes for literal text strings.
It’s not the most efficient code, it transforms the path 2 times to get to it’s final form. I highly doubt you will be creating thousands of folders at a time, so this should be sufficient.
=======================================
“cmd /c md ” &
Quote (
Let (
[
~FilePath = Substitute ( Get ( FilePath ) ; "/" ; ¶ );
~ValuePath = MiddleValues ( ~FilePath ; 2 ; ValueCount ( ~FilePath ) - 2 )
] ;
Substitute ( ~ValuePath ; ¶ ; “/” ) & “YOUR_NEW_FOLDER”
)
)
=======================================
January 13, 2012 at 12:13 AM
Hey Josh!
The calculation works absolutely great and I also used the Let part for creating my invoice file name.
Thank you very much for the code! I just love to get new information to pick apart and firgure out exactly how it works! Thanks to your code here I now have a better understanding of the Let, Substitute, and Left/Right/MiddleValues!
Here’s how I used it for exporting a pdf:
Let (
[
~Client = Substitute ( Left ( Invoices::Client_Name; 9 ) ; " " ; "" );
~City = Substitute ( Invoices::Shipping_City ; " " ; "" );
~Author = Substitute ( Invoices::calc_Author ; " " ; ¶ );
~AuthorFirstName = LeftValues ( ~Author ; 1 );
~Invoice# = Invoices::calc_Invoice#;
~Year = Right ( Get ( CurrentDate ) ; 4 );
~FilePath = Substitute ( Get ( FilePath ) ; "/" ; ¶ );
~ValuePath = MiddleValues ( ~FilePath ; 2 ; ValueCount ( ~FilePath ) - 2 )
] ;
“filewin:/” & Substitute ( ~ValuePath ; ¶ ; “/” ) & “Company Name Invoices/Company Name Invoices ” & ~Year & “/” & ~Invoice# & ~Client & ~City & Substitute ( ~AuthorFirstName ; ¶ ; “” ) & “.pdf”
)
Returns:
filewin:/C:/FilePath/Company Name Invoices/Company Name Invoices 2012/1201001ClientCityAuthor.pdf
I’m using this for an invoicing/payment/reporting solution I’m creating for my dad’s book distribution business.
Cheers!
January 17, 2012 at 3:04 AM