KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Create a folder hierarchy with file and folder object instantiation
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: February 6, 2023

With Folder objects, you can create folders directly on your machine’s disk by using the .create() function:

var $path : Text
var $folder : Object

$path:=Get 4D folder(Database folder)+"Resources"+Folder separator+"Subfolder1"
$folder:=Folder($path; fk platform path)
$folder.create()


While you may instantiate a new folder for each subfolder you would like to create in the folder hierarchy, the same thing can be done in just one line. The only change needed is to add an extension to the path parameter of either the Folder or File command; here, the names of the subfolders are defined from the highest to lowest levels of the file branch. These commands detect whether each subfolder exists on the disk, and if it is not, it will automatically create it.

Folder command
var $path : Text
var $folder : Object

$path:=Get 4D folder(Database folder)+"Resources"+Folder separator
If (Test path name($path)=Is a folder)
   $path:=$path+"Subfolder1"+Folder separator+"Subfolder2"+Folder separator+"Subfolder3"
   $folder:=Folder($path; fk platform path)
   $folder.create()
End if


File command
var $path : Text
var $file : Object

$path:=Get 4D folder(Database folder)+"Resources"+Folder separator
If (Test path name($path)=Is a folder)
   $path:=$path+"Subfolder1"+Folder separator+"Subfolder2"+Folder separator+"new.txt"
   $file:=File($path; fk platform path)
   $file.create()
End if


It is important to note that only creating a Folder (or File) object in the context of 4D does not directly make any changes to the disk; the .create() function must be used in order to create the folder on the disk.

Commented by Tammo Fornalik on March 1, 2023 at 9:32 AM
Jesus, line seperators are removed in comments. Hope you get the point, though.
Commented by Tammo Fornalik on March 1, 2023 at 9:30 AM
By the way, I'm logged in as Michael Höhne, but this site shows "Notes entered by: Tammo Fornalik". He is a colleague of mine, but I'm certainly not him.
Commented by Tammo Fornalik on March 1, 2023 at 9:28 AM
Why would you use Folder sepearator with File and Folder objects? And why use fk platform path to create the folder? $path:=Get 4D folder(Database folder) "Resources" Folder separator "Subfolder1" $folder:=Folder($path; fk platform path) $folder.create() Shouldn't that simply be Folder(fk database folder).folder("Resources").folder("Subfolder1").create() ? Even worse for the following samples: var $path : Text var $folder : Object $path:=Get 4D folder(Database folder) "Resources" Folder separator If (Test path name($path)=Is a folder) $path:=$path "Subfolder1" Folder separator "Subfolder2" Folder separator "Subfolder3" $folder:=Folder($path; fk platform path) $folder.create() End if -> Folder(fk database folder).folder("Resources").folder("Subfolder1").folder("Subfolder2").folder("Subfolder3").create() The same applies to the file sample. It seems that you simply copied an older sample code using CREATE FOLDER and CREATE DOCUMENT and replaced these commands. There is no obvious benefit in using File and Folder given your samples. There is a huge benefit when using the class syntax though.