Official Content

Directory is a data type that allows the management of system directories. It simplifies the development of applications that need to execute actions over system directories and files.

Use Directory data type for typical operations such as creating, copying, moving, renaming and deleting directories. You can also use the Directory data type to obtain the directory attributes such as modification date, time and size.

By defining variables based on this data type you are able to set and use the following properties and methods.

Properties

  • Source
Selects the directory in the specified path.
Returns the result of the last operation. It has one of the following values:
0: Operation completed successfully.
1: Invalid directory instance - when the Source property was not set.
2: Directory does not exist.
3: Directory already exists.
4: Directory not empty.
100: Security error.
-1: Undefined error
Returns the result description of the last operation

Static properties

  • ApplicationDataPath
The directory where files related to the application are stored.
  • TemporaryFilesPath
The directory where temporary files of the application are stored.
C# - It is implemented using Path.GetTempPath() method.
Java - It returns the value of System.getProperty("java.io.tmpdir").
  • ExternalFilesPath
The path to the external storage device, if it exists, otherwise this property has the value of the property "ApplicationDataPath".
  • CacheFilesPath
The directory where the cache files of the application are stored.

For more information about the static properties of this data type please refer to Directory Data Type Static properties.

Note: CacheFilesPath are available since GeneXus 16 upgrade 4 for iOS generator.

CacheFilesPath are available since GeneXus 16 upgrade 9 for Android generator.

Methods

Create

Creates a new directory in the specified source.

Parameters

Directory name (character)

Sample
&directory.Source="c:\temp"
&directory.create()

Delete

Deletes the source directory.

Sample
&directory.delete()

Exists

Checks whether the directory specified in source exists.

Returned values

Boolean

Sample
If &directory.exists() 
    Msg("The directory exists!")
Else
    Msg("The directory does not exist!")
EndIf

GetName

Returns the source directory's name.

Returned values

Character

Sample
&directory.Source = "c:\temp"
Msg(&directory.GetName()) // screen shows: temp

GetAbsoluteName

Returns the absolute name of the source directory.

Returned values

Character

Sample
&directory.Source="c:\temp"
Msg(&directory.GetAbsoluteName()) // screen shows: c:\temp

Rename

Renames the source directory. It can be also used to move the directory changing its current path.

Parameters

The new name of the directory (Character).

Sample
&directory.Rename("d:\temp2")

GetFiles

Returns the names of files in the source directory. Also, a filter can be specified using extension-wildcards or end-with patterns.

Parameters

Filter (Character)

Sample
for &auxFile in &directory.GetFiles('*.txt')
   // do something with .txt auxFile 
endfor

where &auxFile is defined as a File variable.

GetDirectories

Returns the names of every directory in the source directory.

Sample
for &auxDir in &directory.GetDirectories()
  // do something with auxDir
endfor

where &auxDir is defined as a Directory variable.

Directory Data Type and External Storage

Directory data type handles directories 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 directory located in the external storage.

Examples when using External Storage

Suppose you have a bucket named 'mytest' and a folder named 'petsFolder' created using the Storage Provider API, and use StorageAPI.GetDirectory(). Here is a list of the output of each method:

Method Returns
GetName petsFolder
GetAbsoluteName mytest:\petsFolder\


Refer to Storage Provider API for related sample code.

Restrictions

Mapped Drives and Network Drives are not supported in web environments

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

Scope

Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant