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.
Example
The following example creates a preferences file in the project folder:
var $created : Boolean
$created:=File("/PACKAGE/SpecialPrefs/"+Current user+".myPrefs").create()
Pathnames
File
objects support several pathnames, including filesystems
or posix
syntax. Supported pathnames are detailed in the Pathnames page.
File object
.copyTo( destinationFolder : 4D.Folder { ; newName : Text } { ; overwrite : Integer } ) : 4D.File copies the File object into the specified destinationFolder |
.create() : Boolean creates a file on disk according to the properties of the File object |
.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) |
.getAppInfo() : Object returns the contents of a .exe, .dll or .plist file information as an object |
.getContent( ) : 4D.Blobreturns a 4D.Blob object containing the entire content of a file |
.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 File object into the specified destinationFolder |
.name : Text the name of the file without extension (if any) |
.open( { mode : Text } ) : 4D.FileHandle .open( { options : Object } ) : 4D.FileHandle creates and returns a new 4D.FileHandle object on the file, in the specified mode or with the specified options |
.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 File object |
.setAppInfo( info : Object ) writes the info properties as information contents of a .exe, .dll or .plist file |
.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 |
4D.File.new()
History
Release | Changes |
---|---|
18 R6 | Added |
4D.File.new ( path : Text { ; pathType : Integer } ) : 4D.File
4D.File.new ( fileConstant : Integer ) : 4D.File
Description
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
Release | Changes |
---|---|
17 R5 | Added |
.copyTo( destinationFolder : 4D.Folder { ; newName : Text } { ; overwrite : Integer } ) : 4D.File
Parameter | Type | Description | |
---|---|---|---|
destinationFolder | 4D.Folder | -> | Destination folder |
newName | Text | -> | Name for the copy |
overwrite | Integer | -> | fk overwrite to replace existing elements |
Result | 4D.File | <- | Copied file |
Description
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 | Value | Comment |
---|---|---|
fk overwrite | 4 | Overwrite existing elements, if any |
Returned value
The copied File
object.
Example
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
Release | Changes |
---|---|
17 R5 | Added |
Not available for ZIP archives
.create() : Boolean
Parameter | Type | Description | |
---|---|---|---|
Result | Boolean | <- | True if the file was created successfully, false otherwise |
Description
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.
Returned value
- True if the file is created successfully;
- False if a file with the same name already exists or if an error occured.
Example
Creation of a preferences file in the database folder:
var $created : Boolean
$created:=File("/PACKAGE/SpecialPrefs/"+Current user+".myPrefs").create()
.createAlias()
History
Release | Changes |
---|---|
17 R5 | Added |
.createAlias( destinationFolder : 4D.Folder ; aliasName : Text { ; aliasType : Integer } ) : 4D.File
Parameter | Type | Description | |
---|---|---|---|
destinationFolder | 4D.Folder | -> | Destination folder for the alias or shortcut |
aliasName | Text | -> | Name of the alias or shortcut |
aliasType | Integer | -> | Type of the alias link |
Result | 4D.File | <- | Alias or shortcut file reference |
Description
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 | Value | Comment |
---|---|---|
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.
Example
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
Release | Changes |
---|---|
17 R5 | Added |
.creationDate : Date
Description
The .creationDate
property returns the creation date of the file.
This property is read-only.
.creationTime
History
Release | Changes |
---|---|
17 R5 | Added |
.creationTime : Time