File and Folder functions, properties, and commands allow you to handle files and folders as objects. This makes file and folder management powerful and flexible. For example, to create a new file in the current user's Documents folder, you can write:
$ok:=Folder(fk documents folder).file("Archives/John4D.prefs").create()
In addition, file and folder objects support
fileSystems, which provide contextual path to main application folders.
4D accepts several
filesystem pathnames that designate specific 4D folders with variable location on macOS and Windows. Filesystem pathnames are useful for two main reasons:
- Independence: You can move your solution from one place to another regardless of the OS, without having to worry about paths,
- Security: No code can access elements located above the file system root on the disk (sandboxing).
The following filesystem pathnames are supported:
|"/DATA"||Current data folder|
|"/PACKAGE"||Project root folder (with or without 4dbase extension)|
|"/RESOURCES"||Current project resources folder|
|"/SOURCES"||Current project sources folder|
With this syntax:
- folders are separated by "/"
- absolute pathnames start with a "/"
- to move up one folder in a relative path, use "../" in front of the pathname (for security, you cannot move up the filesystem).
Platform-specific syntax depends on the operating system on which the command is executed. Note that when creating a file or folder object with this syntax, you must declare it using the
fk platform path constant as parameter.
The following patterns are supported:
- folder separators are "\"
- the text contains ':' and '\' as the second and third character,
- the text starts with "\".
$ok:=Folder("C:\\Monday";fk platform path).create()
$ok:=Folder("\\\\svr-internal\\tempo";fk platform path).create()
Entering Windows pathnames and escape sequences
The 4D code editor allows the use of escape sequences. An escape sequence is a set of characters that are used to replace a "special" character. The sequence begins with a backslash
\, followed by a character. For example,
\t is the escape sequence for the
\ character is also used as the separator in pathnames in Windows. In general, 4D will correctly interpret Windows pathnames that are entered in the code editor by replacing single backslashes
\ with double backslashes
\\. For example,
C:\Folder will become
However, if you write
C:\MyDocuments\New, 4D will display
C:\\MyDocuments\New. In this case, the second
\ is incorrectly interpreted as
\N (an existing escape sequence). You must therefore enter a double
\\ when you want to insert a backslash before a character that is used in one of the escape sequences recognized by 4D.
The following escape sequences are recognized by 4D:
|Escape sequence||Character replaced|
|LF (New line)|
|HT (Horizontal tab)|
|CR (Carriage return)|
The following patterns are supported (HFS+ syntax):
- folder separators are ":"
- the path must not start with a ":"
$ok:=Folder("macintosh hd:";fk platform path).create()
$ok:=Folder("Monday:Tuesday";fk platform path).create() //a volume must be called Monday
Absolute and relative pathnames
$ko:=Folder("myFolder").create() //relative pathname with constructor
If you want to handle files or folders in various locations (project folder, system folders, etc.), you can use
filesystems (see above). For example, you can write:
$okFolder:=Folder("/PACKAGE/myFolder").create() //folder created at the structure level
$okFile:=File("/DATA/Prefs/tempo.txt").create() //file created in the data folder
.folder() folder methods
//to reference a "Picture" folder within the user documents folder
$userImages:=Folder(fk documents folder).folder("Pictures")
//to create a folder on the desktop
$ok:=Folder(fk desktop folder).folder("myFolder").create()
Absolute pathnames are not supported and will return errors.
The flexibility of file and folder functions offers you various possibilities for handling files and folders, like in the following examples:
$f:=Folder(fk desktop folder).folder("archive/jan2019")
$f5:=File("c:\\archives\\local\\jan2019.txt";fk platform path)
$f6:=File(fk backup log file)