File
File
objects are created with the File
command. They contain references to disk files that may or may not actually exist on disk. For example, when you execute the File
command to create a new file, a valid File
object is created but nothing is actually stored on disk until you call the file.create( )
function.
Beispiel
The following example creates a preferences file in the project folder:
var $created : Boolean
$created:=File("/PACKAGE/SpecialPrefs/"+Current user+".myPrefs").create()
File object
.copyTo( destinationFolder : 4D.Folder { ; newName : Text } { ; overwrite : Integer } ) : 4D.File copies the |
.create() : Boolean creates a file on disk according to the properties of the |
.createAlias( destinationFolder : 4D.Folder ; aliasName : Text { ; aliasType : Integer } ) : 4D.File creates an alias (macOS) or a shortcut (Windows) |
.creationDate : Date the creation date of the file |
.creationTime : Time the creation time of the file |
.delete( ) deletes the file |
.exists : Boolean true if the file exists on disk |
.extension : Text the extension of the file name (if any) |
.fullName : Text the full name of the file, including its extension (if any) |
.getContent( ) : Blob returns a |
.getIcon( { size : Integer } ) : Picture the icon of the file |
.getText( { charSetName : Text } { ; } { breakMode : integer} ) : Text .getText( { charSetNum : integer } { ; } { breakMode : integer} ) : Text returns the contents of the file as text |
.hidden : Boolean true if the file is set as "hidden" at the system level |
.isAlias : Boolean true if the file is an alias, a shortcut, or a symbolic link |
.isFile : Boolean always true for a file |
.isFolder : Boolean always false for a file |
.isWritable : Boolean true if the file exists on disk and is writable |
.modificationDate : Date the date of the file's last modification |
.modificationTime : Time the time of the file's last modification |
.moveTo( destinationFolder : 4D.Folder { ; newName : Text } ) : 4D.File moves or renames the |
.name : Text the name of the file without extension (if any) |
.original : 4D.File .original : 4D.Folder the target element for an alias, a shortcut, or a symbolic link file |
.parent : 4D.Folder the parent folder object of the file |
.path : Text the POSIX path of the file |
.platformPath : Text the path of the file expressed with the current platform syntax |
.rename( newName : Text ) : 4D.File renames the file with the name you passed in newName and returns the renamed |
.setContent ( content : Blob ) rewrites the entire content of the file using the data stored in the content BLOB |
.setText ( text : Text {; charSetName : Text { ; breakMode : Integer } } ) .setText ( text : Text {; charSetNum : Integer { ; breakMode : Integer } } ) writes text as the new contents of the file |
.size : Real the size of the file expressed in bytes |
File
History
Version | Changes |
---|---|
v17 R5 | Added |
File ( path : Text { ; pathType : Integer }{ ; * } ) : 4D.File
File ( fileConstant : Integer { ; * } ) : 4D.File
Parameter | Typ | Beschreibung | |
---|---|---|---|
path | Text | -> | File path |
fileConstant | Ganzzahl | -> | 4D file constant |
pathType | Ganzzahl | -> | fk posix path (default) or fk platform path |
* | -> | * to return file of host database | |
Ergebnis | 4D.File | <- | New file object |
Beschreibung
The File
command creates and returns a new object of the 4D.File
type. The command accepts two syntaxes:
File ( path { ; pathType } { ; * })
In the path parameter, pass a file path string. You can use a custom string or a filesystem (e.g., "/DATA/myfile.txt").
Only absolute pathnames are supported with the
File
command.
By default, 4D expects a path expressed with the POSIX syntax. If you work with platform pathnames (Windows or macOS), you must declare it using the pathType parameter. The following constants are available:
Constant | Wert | Kommentar |
---|---|---|
fk platform path | 1 | Path expressed with a platform-specific syntax (mandatory in case of platform pathname) |
fk posix path | 0 | Path expressed with POSIX syntax (default) |
File ( fileConstant { ; * } )
In the fileConstant parameter, pass a 4D built-in or system file, using one of the following constants:
Constant | Wert | Kommentar |
---|---|---|
Backup history file | 19 | Backup history file (see Configuration and trace files). Stored in the backup destination folder. |
Backup log file | 13 | Current backup journal file. Stored in the application Logs folder. |
Backup settings file | 1 | Default backup.4DSettings file (xml format), stored in the Settings folder of the project |
Backup settings file for data | 17 | backup.4DSettings file (xml format) for the data file, stored in the Settings folder of the data folder |
Build application log file | 14 | Current log file in xml format of the application builder. Stored in the Logs folder. |
Build application settings file | 20 | Default settings file of the application builder ("buildApp.4DSettings"). Stored in the Settings folder of the project. |
Compacting log file | 6 | Log file of the most recent compacting done with the Compact data file command or the Maintenance and security center. Stored in the Logs folder. |
Current backup settings file | 18 | backup.4DSettings file currently used by the application. It can be the backup settings file (default) or a custom user backup settings file defined for the data file |
Debug log file | 12 | Log file created by the SET DATABASE PARAMETER(Debug log recording) command. Stored in the Logs folder. |
Diagnostic log file | 11 | Log file created by the SET DATABASE PARAMETER(Diagnostic log recording) command. Stored in the Logs folder. |
Directory file | 16 | directory.json file, containing the description of users and groups (if any) for the project application. It can be located either in the user settings folder (default, global to the project), or in the data settings folder (specific to a data file). |
HTTP debug log file | 9 | Log file created by the WEB SET OPTION(Web debug log) command. Stored in the Logs folder. |
HTTP log file | 8 | Log file created by the WEB SET OPTION(Web log recording) command. Stored in Logs folder. |
Last backup file | 2 | Last backup file, named <applicationName>[bkpNum].4BK, stored at a custom location. |
Repair log file | 7 | Log file of database repairs made on the database in the Maintenance and Security Center (MSC). Stored in the Logs folder. |
Request log file | 10 | Standard client/server request log file (excluding Web requests) created by the SET DATABASE PARAMETER(4D Server log recording) or SET DATABASE PARAMETER(Client log recording) commands. If executed on the server, the server log file is returned (stored in the Logs folder on the server). If executed on the client, the client log file is returned (stored in the client local Logs folder). |
SMTP log file | 15 | Log file created by the SET DATABASE PARAMETER(SMTP Log) command. Stored in the Logs folder. |
User settings file | 3 | settings.4DSettings file for all data files, stored in Preferences folder next to structure file if enabled. |
User settings file for data | 4 | settings.4DSettings file for current data file, stored in Preferences folder next to the data file. |
Verification log file | 5 | Log files created by the VERIFY CURRENT DATA FILE and VERIFY DATA FILE commands or the Maintenance and Security Center (MSC). Stored in the Logs folder. |
If the target fileConstant does not exist, a null object is returned. No errors are raised.
If the command is called from a component, pass the optional * parameter to get the path of the host database. Otherwise, if you omit the * parameter, a null object is always returned.
4D.File.new()
History
Version | Changes |
---|---|
v18 R6 | Added |
4D.File.new ( path : Text { ; pathType : Integer }{ ; * } ) : 4D.File
4D.File.new ( fileConstant : Integer { ; * } ) : 4D.File
Beschreibung
The 4D.File.new()
function creates and returns a new object of the 4D.File
type. It is identical to the File
command (shortcut).
It is recommended to use the
File
shortcut command instead of4D.File.new()
.
.copyTo()
History
Version | Changes |
---|---|
v17 R5 | Added |
.copyTo( destinationFolder : 4D.Folder { ; newName : Text } { ; overwrite : Integer } ) : 4D.File
Parameter | Typ | Beschreibung | |
---|---|---|---|
destinationFolder | 4D.Folder | -> | Destination folder |
newName | Text | -> | Name for the copy |
overwrite | Ganzzahl | -> | fk overwrite to replace existing elements |
Ergebnis | 4D.File | <- | Copied file |
Beschreibung
The .copyTo()
function copies the File
object into the specified destinationFolder .
The destinationFolder must exist on disk, otherwise an error is generated.
By default, the file is copied with the name of the original file. If you want to rename the copy, pass the new name in the newName parameter. The new name must comply with naming rules (e.g., it must not contain characters such as ":", "/", etc.), otherwise an error is returned.
If a file with the same name already exists in the destinationFolder, by default 4D generates an error. You can pass the fk overwrite
constant in the overwrite parameter to ignore and overwrite the existing file
Constant | Wert | Kommentar |
---|---|---|
fk overwrite | 4 | Overwrite existing elements, if any |
Rückgabewert
The copied File
object.
Beispiel
You want to copy a picture file from the user's document folder to the application folder:
var $source; $copy : Object
$source:=Folder(fk documents folder).file("Pictures/photo.png")
$copy:=$source.copyTo(Folder("/PACKAGE");fk overwrite)
.create()
History
Version | Changes |
---|---|
v17 R5 | Added |
Not available for ZIP archives
.create() : Boolean
Parameter | Typ | Beschreibung | |
---|---|---|---|
Ergebnis | Boolean | <- | True if the file was created successfully, false otherwise |
Beschreibung
The .create()
function creates a file on disk according to the properties of the File
object.
If necessary, the function creates the folder hierachy as described in the platformPath or path properties. If the file already exists on disk, the function does nothing (no error is thrown) and returns false.
Rückgabewert
- True if the file is created successfully;
- False if a file with the same name already exists or if an error occured.
Beispiel
Creation of a preferences file in the database folder:
var $created : Boolean
$created:=File("/PACKAGE/SpecialPrefs/"+Current user+".myPrefs").create()
.createAlias()
History
Version | Changes |
---|---|
v17 R5 | Added |
.createAlias( destinationFolder : 4D.Folder ; aliasName : Text { ; aliasType : Integer } ) : 4D.File
Parameter | Typ | Beschreibung | |
---|---|---|---|
destinationFolder | 4D.Folder | -> | Destination folder for the alias or shortcut |
aliasName | Text | -> | Name of the alias or shortcut |
aliasType | Ganzzahl | -> | Type of the alias link |
Ergebnis | 4D.File | <- | Alias or shortcut file reference |
Beschreibung
The .createAlias()
function creates an alias (macOS) or a shortcut (Windows) to the file with the specified aliasName name in the folder designated by the destinationFolder object.
Pass the name of the alias or shortcut to create in the aliasName parameter.
By default on macOS, the function creates a standard alias. You can also create a symbolic link by using the aliasType parameter. The following constants are available:
Constant | Wert | Kommentar |
---|---|---|
fk alias link | 0 | Alias link (default) |
fk symbolic link | 1 | Symbolic link (macOS only) |
On Windows, a shortcut (.lnk file) is always created (the aliasType parameter is ignored).
Returned object
A 4D.File
object with the isAlias
property set to true.
Beispiel
You want to create an alias to a file in your database folder:
$myFile:=Folder(fk documents folder).file("Archives/ReadMe.txt")
$aliasFile:=$myFile.createAlias(File("/PACKAGE");"ReadMe")
.creationDate
History
Version | Changes |
---|---|
v17 R5 | Added |
.creationDate : Date
Beschreibung
The .creationDate
property returns the creation date of the file.
This property is read-only.
.creationTime
History
Version | Changes |
---|---|
v17 R5 | Added |
.creationTime : Time
Beschreibung
The .creationTime
property returns the creation time of the file (expressed as a number of seconds beginning at 00:00).
This property is read-only.
.delete()
History
Version | Changes |
---|---|
v17 R5 | Added |
.delete( )
| Parameter | Typ | | Beschreibung | | --------- | --- | | ------------------------------- | | | | | Does not require any parameters |
Beschreibung
The .delete()
function deletes the file.
If the file is currently open, an error is generated.
If the file does not exist on disk, the function does nothing (no error is generated).
WARNING:
.delete( )
can delete any file on a disk. This includes documents created with other applications, as well as the applications themselves..delete( )
should be used with extreme caution. Deleting a file is a permanent operation and cannot be undone.
Beispiel
You want to delete a specific file in the database folder:
$tempo:=File("/PACKAGE/SpecialPrefs/"+Current user+".prefs")
If($tempo.exists)
$tempo.delete()
ALERT("User preference file deleted.")
End if
.exists
History
Version | Changes |
---|---|
v17 R5 | Added |
.exists : Boolean
Beschreibung
The .exists
property returns true if the file exists on disk, and false otherwise.
This property is read-only.
.extension
History
Version | Changes |
---|---|
v17 R5 | Added |
.extension : Text
Beschreibung
The .extension
property returns the extension of the file name (if any). An extension always starts with ".". The property returns an empty string if the file name does not have an extension.
This property is read-only.
.fullName
History
Version | Changes |
---|---|
v17 R5 | Added |
.fullName : Text
Beschreibung
The .fullName
property returns the full name of the file, including its extension (if any).
This property is read-only.
.getContent()
History
Version | Changes |
---|---|
v17 R5 | Added |
.getContent( ) : Blob
Parameter | Typ | Beschreibung | |
---|---|---|---|
Ergebnis | Blob | <- | File content |
Beschreibung
The .getContent()
function returns a BLOB
containing the entire content of a file. For information on BLOBs, please refer to the BLOB section.
Rückgabewert
A Blob
.
Beispiel
To save a document's contents in a BLOB
field:
var $vPath : Text
$vPath:=Select document("";"*";"Select a document";0)
If(OK=1) //If a document has been chosen
[aTable]aBlobField:=File($vPath;fk platform path).getContent()
End if
.getIcon()
History
Version | Changes |
---|---|
v17 R5 | Added |
.getIcon( { size : Integer } ) : Picture
Parameter | Typ | Beschreibung | |
---|---|---|---|
size | Ganzzahl | -> | Side length for the returned picture (pixels) |
Ergebnis | Bild | <- | Icon |
Beschreibung
The .getIcon()
function returns the icon of the file.
The optional size parameter specifies the dimensions in pixels of the returned icon. This value actually represents the length of the side of the square containing the icon. Icons are usually defined in 32x32 pixels (“large icons”) or 16x16 pixels (“small icons”). If you pass 0 or omit this parameter, the "large icon" version is returned.
If the file does not exist on disk, a default blank icon is returned.
Rückgabewert
File icon picture.
.getText()
History
Version | Changes |
---|---|
v17 R5 | Added |
.getText( { charSetName : Text } { ; } { breakMode : integer} ) : Text
.getText( { charSetNum : integer } { ; } { breakMode : integer} ) : Text
Parameter | Typ | Beschreibung | |
---|---|---|---|
charSetName | Text | -> | Name of character set |
charSetNum | Ganzzahl | -> | Number of character set |
breakMode | Ganzzahl | -> | Processing mode for line breaks |
Ergebnis | Text | <- | Text from the document |
Beschreibung
The .getText()
function returns the contents of the file as text .
Optionally, you can designate the character set to be used for reading the contents. You can pass either:
- in charSetName, a string containing the standard set name (for example "ISO-8859-1" or ""UTF-8"),
- or in charSetNum, the MIBEnum ID (number) of the standard set name.
For the list of character sets supported by 4D, refer to the description of the
CONVERT FROM TEXT
command.
If the document contains a Byte Order Mark (BOM), 4D uses the character set that it has set instead of the one specified in charSetName or charSetNum (this parameter is then ignored). If the document does not contain a BOM and if charSetName or charSetNum is omitted, by default 4D uses the "UTF-8" character set.
In breakMode, you can pass a number indicating the processing to apply to end-of-line characters in the document. The following constants of the "System Documents" theme are available:
Constant | Wert | Kommentar |
---|---|---|
Document unchanged | 0 | No processing |
Document with native format | 1 | (Default) Line breaks are converted to the native format of the operating system: CR (carriage return) under OS X, CRLF (carriage return + line feed) under Windows |
Document with CRLF | 2 | Line breaks are converted to Windows format: CRLF (carriage return + line feed) |
Document with CR | 3 | Line breaks are converted to OS X format: CR (carriage return) |
Document with LF | 4 | Line breaks are converted to Unix format: LF (line feed) |
By default, when you omit the breakMode parameter, line breaks are processed in native mode (1).
Rückgabewert
Text of the file.
Beispiel
Given the following text document (fields are separated by tabs):
id name price vat
3 thé 1.06€ 19.6
2 café 1.05€ 19.6
When you execute this code:
$myFile:=Folder(fk documents folder).file("Billing.txt") //UTF-8 by default
$txt:=$myFile.getText()
... you get:
// $Text = "id name price vat\r\n3 thé 1.06€\t19.6\r\n2\tcafé\t1.05€\t19.6"
// \t = tab
// \r = CR
.hidden
History
Version | Changes |
---|---|
v17 R5 | Added |
.hidden : Boolean
Beschreibung
The .hidden
property returns true if the file is set as "hidden" at the system level, and false otherwise.
This property is read-only.
.isAlias
History
Version | Changes |
---|---|
v17 R5 | Added |
.isAlias : Boolean
Beschreibung
The .isAlias
property returns true if the file is an alias, a shortcut, or a symbolic link, and false otherwise.
This property is read-only.
.isFile
History
Version | Changes |
---|---|
v17 R5 | Added |
.isFile : Boolean
Beschreibung
The .isFile
property returns always true for a file.
This property is read-only.
.isFolder
History
Version | Changes |
---|---|
v17 R5 | Added |
.isFolder : Boolean
Beschreibung
The .isFolder
property returns always false for a file.
This property is read-only.
.isWritable
History
Version | Changes |
---|---|
v17 R5 | Added |
.isWritable : Boolean
Beschreibung
The .isWritable
property returns true if the file exists on disk and is writable.
The property checks the ability of the 4D application to write on the disk (access rights), it does not solely rely on the writable attribute of the file.
This property is read-only.
Beispiel
$myFile:=File("C:\\Documents\\Archives\\ReadMe.txt";fk platform path)
If($myFile.isWritable)
$myNewFile:=$myFile.setText("Added text")
End if
.modificationDate
History
Version | Changes |
---|---|
v17 R5 | Added |
.modificationDate : Date
Beschreibung
The .modificationDate
property returns the date of the file's last modification.
This property is read-only.
.modificationTime
History
Version | Changes |
---|---|
v17 R5 | Added |
.modificationTime : Time
Beschreibung
The .modificationTime
property returns the time of the file's last modification (expressed as a number of seconds beginning at 00:00).
This property is read-only.
.moveTo()
History
Version | Changes |
---|---|
v17 R5 | Added |
.moveTo( destinationFolder : 4D.Folder { ; newName : Text } ) : 4D.File
Parameter | Typ | Beschreibung | |
---|---|---|---|
destinationFolder | 4D.Folder | -> | Destination folder |
newName | Text | -> | Full name for the moved file |
Ergebnis | 4D.File | <- | Moved file |
Beschreibung
The .moveTo()
function moves or renames the File
object into the specified destinationFolder.
The destinationFolder must exist on disk, otherwise an error is generated.
By default, the file retains its name when moved. If you want to rename the moved file, pass the new full name in the newName parameter. The new name must comply with naming rules (e.g., it must not contain characters such as ":", "/", etc.), otherwise an error is returned.
Returned object
The moved File
object.
Beispiel
$DocFolder:=Folder(fk documents folder)
$myFile:=$DocFolder.file("Current/Infos.txt")
$myFile.moveTo($DocFolder.folder("Archives");"Infos_old.txt")
.name
History
Version | Changes |
---|---|
v17 R5 | Added |
.name : Text
Beschreibung
The .name
property returns the name of the file without extension (if any).
This property is read-only.
.original
History
Version | Changes |
---|---|
v17 R5 | Added |
.original : 4D.File
.original : 4D.Folder
Beschreibung
The .original
property returns the target element for an alias, a shortcut, or a symbolic link file. The target element can be:
- a file object
- a folder object
For non-alias files, the property returns the same file object as the file.
This property is read-only.
.parent
History
Version | Changes |
---|---|
v17 R5 | Added |
.parent : 4D.Folder
Beschreibung
The .parent
property returns the parent folder object of the file. If the path represents a system path (e.g., "/DATA/"), the system path is returned.
This property is read-only.
.path
History
Version | Changes |
---|---|
v17 R5 | Added |
.path : Text
Beschreibung
The .path
property returns the POSIX path of the file. If the path represents a filesystem (e.g., "/DATA/"), the filesystem is returned.
This property is read-only.
.platformPath
History
Version | Changes |
---|---|
v17 R5 | Added |
.platformPath : Text
Beschreibung
The .platformPath
property returns the path of the file expressed with the current platform syntax.
This property is read-only.
.rename()
History
Version | Changes |
---|---|
v17 R5 | Added |
.rename( newName : Text ) : 4D.File
Parameter | Typ | Beschreibung | |
---|---|---|---|
newName | Text | -> | New full name for the file |
Ergebnis | 4D.File | <- | Renamed file |
Beschreibung
The .rename()
function renames the file with the name you passed in newName and returns the renamed File
object.
The newName parameter must comply with naming rules (e.g., it must not contain characters such as ":", "/", etc.), otherwise an error is returned. If a file with the same name already exists, an error is returned.
Note that the function modifies the full name of the file, i.e. if you do not pass an extension in newName, the file will have a name without an extension.
Returned object
The renamed File
object.
Beispiel
You want to rename "ReadMe.txt" in "ReadMe_new.txt":
$toRename:=File("C:\\Documents\\Archives\\ReadMe.txt";fk platform path)
$newName:=$toRename.rename($toRename.name+"_new"+$toRename.extension)
.setContent()
History
Version | Changes |
---|---|
v17 R5 | Added |
.setContent ( content : Blob )
Parameter | Typ | Beschreibung | |
---|---|---|---|
content | BLOB | -> | New contents for the file |
Beschreibung
The .setContent( )
function rewrites the entire content of the file using the data stored in the content BLOB. For information on BLOBs, please refer to the BLOB section.
Beispiel
$myFile:=Folder(fk documents folder).file("Archives/data.txt")
$myFile.setContent([aTable]aBlobField)
.setText()
History
Version | Changes |
---|---|
v17 R5 | Added |
.setText ( text : Text {; charSetName : Text { ; breakMode : Integer } } )
.setText ( text : Text {; charSetNum : Integer { ; breakMode : Integer } } )
Parameter | Typ | Beschreibung | |
---|---|---|---|
Text | Text | -> | Text to store in the file |
charSetName | Text | -> | Name of character set |
charSetNum | Ganzzahl | -> | Number of character set |
breakMode | Ganzzahl | -> | Processing mode for line breaks |
Beschreibung
The .setText()
function writes text as the new contents of the file.
If the file referenced in the File
object does not exist on the disk, it is created by the function. When the file already exists on the disk, its prior contents are erased, except if it is already open, in which case, its contents are locked and an error is generated.
In text, pass the text to write to the file. It can be a literal ("my text"), or a 4D text field or variable.
Optionally, you can designate the character set to be used for writing the contents. You can pass either:
- in charSetName, a string containing the standard set name (for example "ISO-8859-1" or ""UTF-8"),
- or in charSetNum, the MIBEnum ID (number) of the standard set name.
For the list of character sets supported by 4D, refer to the description of the
CONVERT FROM TEXT
command.
If a Byte Order Mark (BOM) exists for the character set, 4D inserts it into the file. If you do not specify a character set, by default 4D uses the "UTF-8" character set and a BOM.
In breakMode, you can pass a number indicating the processing to apply to end-of-line characters before saving them in the file. The following constants, found in the System Documents theme are available:
Constant | Wert | Kommentar |
---|---|---|
Document unchanged | 0 | No processing |
Document with native format | 1 | (Default) Line breaks are converted to the native format of the operating system: CR (carriage return) in macOS, CRLF (carriage return + line feed) in Windows |
Document with CRLF | 2 | Line breaks are converted to Windows format: CRLF (carriage return + line feed) |
Document with CR | 3 | Line breaks are converted to OS X format: CR (carriage return) |
Document with LF | 4 | Line breaks are converted to Unix format: LF (line feed) |
By default, when you omit the breakMode parameter, line breaks are processed in native mode (1).
Beispiel
$myFile:=File("C:\\Documents\\Hello.txt";fk platform path)
$myFile.setText("Hello world")
.size
History
Version | Changes |
---|---|
v17 R5 | Added |
.size : Real
Beschreibung
The .size
property returns the size of the file expressed in bytes. If the file does not exist on disk, the size is 0.
This property is read-only.