File data type

Official Content
This documentation is valid for:


File is a GeneXus data type that allows you to manage system files.

It helps you to develop applications that need to execute actions to the system files.

It can be used for typical operations such as copying, moving, renaming and deleting files. You can also use it to get file attributes such as the last modification date, time and size.

Properties

Source Assigns a path to the file. The source cannot be read, use GetAbsoluteName(*) instead.
Separator  Returns the name separator character depending on the OS.
ErrCode Returns the result of the last operation.
ErrDescription Returns the result description of the last operation.

 

Methods

Copy Copies the specified file to a new file.
Delete Deletes the specified file.
Exists Determines whether the specified file exists.
GetName Returns the name of the specified file.
GetAbsoluteName Returns the full path name of the specified file.
GetPath Returns the path of the parent directory.
GetURI Returns the URI to the specified file. (1)
Rename Renames the specified file.
GetLastModified Returns the date and time the specified file was last modified.
GetLength Gets the size of the specified file.
Open See Text file handling methods.
Close See Text file handling methods.
FromBase64String Write encoded content into the file(2).

Details and samples about the methods


Copy

Copies the file specified in the source to a new location. If the target file already exists, it will be overridden.

Parameter
New filename  (Character)

Example

&file.copy("c:\empcopiedFile.txt")


Delete

Deletes the file specified in the source.

Example

&file.delete()


Exists

Checks whether the file specified in source exists

Returned value
Boolean

Example

If &file.exists()
    msg("The file exists!")
Else 
    msg("The file does not exist!")
EndIf



GetName

Returns the name of the file specified in the source.

Returned value
Character

Example

&file.Source="c:\myFile.txt"
Msg(&file.GetName()) // screen shows: myFile.txt



GetAbsoluteName

Returns the absolute name of the file specified in the source.

Returned value
Character

Example

&file.Source="c:\empmyFile.txt"
Msg(&file.GetAbsoluteName()) // screen shows: c:\empmyFile.txt


GetPath

Returns the path of the parent directory.

Returned value
Character

Example

&file.source = 'c:\temp\folder\emptyFile.txt'
&parentDir = &file.getPath()
Msg(&parentDir) // result: c:\temp\folder

GetURI

Returns the URI to the specified file. For local files, it is equivalent to GetAbsoluteName() but differs in external files (files returned by Storage API methods)

Returned value
Character

Example

&file.Source="c:\empmyFile.txt"
Msg(&file.GetURI()) // screen shows: c:\empmyFile.txt


Rename

Renames the file specified in the source. It can also be used to move the file, changing its current path.

Parameter
New name of the file (Character)

Example

&file.Rename("d:\empnewFileName.txt")



GetLastModified

Returns the date and time the file specified in the source was last modified.

Returned value
DateTime

Example

&dateTime = &file.GetLastModified()



GetLength

Returns the size (in bytes) of the file specified in the source.

Returned value
Numeric

Example

&size = &file.GetLength()



FromBase64String

Convert a Base64-encoded string to a human-readable string, and write it into the file. Optionally, this function returns a boolean value indicating if the operation was successful.

Parameter
Base64-string

Returned value
Boolean

Example

&file.FromBase64String(&Base64String)

This method is not supported for user events in smart devices.  

Error Codes

0 Operation completed successfully.
1 Invalid file instance - if source property was not set.
2 The file does not exist.
3 File already exists.
100 Security error.
-1 Undefined error.


File data type and External Storage

File data type handles files that are in the local file system by default. But when it is returned by a method of the Storage Provider API, then it refers to a file located in the external storage. 

If it is returned by the GetFiles() method of the Directory data type and that is a returned by the GetDirectories() method in the Storage Provider API, then it refers to a file located in the external storage too.

Examples when using External Storage

Suppose you have a bucket named 'mytest' and uploaded the file named 'cat.jpg' to a folder named 'petsFolder', using Storage Provider API.

Method Returns
GetURI https://mytest.s3.amazonaws.com/petsFolder/cat.jpg
GetName cat.jpg
GetAbsoluteName petsFolder/cat.jpg
GetPath petsFolder

Refer to Storage Provider API for related sample code.

Security tips

When a property or method is used to assign a file's path do not use user's inputs concatenations or sanitize the user's entries to avoid path traversal or path manipulation vulnerability risks.

 

See Also

File data type: Text file handling
Storage Provider API


Availability

The file data type is available as of  GeneXus X Evolution.
(1)GetURI is available as of GeneXus 15 Upgrade 9 for Web generators (Java & .Net) and is available as of GeneXus 16 upgrade 4 for Android (user events only) and iOS (full support).
(2) FromBase64 is available as of Genexus 15 Upgrade 10.

As of GeneXus 15 .GIF are supported for iOS.

Scope

Objects Procedure object, Web Panel object, Transaction object, Panel for Smart Devices
Platform Web(.Net, Java), Smart Devices(iOS)
   




Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.