PrevNextUpHome SophiaFramework UNIVERSE 5.3

15.3. Class for Operating a Directory

The SFXDirectory class is used to perform the directory operations such as creating a directory, moving a directory, deleting a directory, retrieving directory attribute information, traversing files and subdirectories in a directory, making a temporary directory etc.

[Caution] About MIF File Setting

Never forget to turn on the File option in the MIF file setting of privilege level.

[Note] Error handling

All the following functions return error values of SFCError type. However, error handling is omitted for simplified explanation.

Example 15.10. Create the directory

SFXDirectory::Create(SFXPath("/dir1/")); // create dir1

// create dir2 if only dir1 exists
SFXDirectory::Create(SFXPath("/dir1/dir2/"));

// if dir1 does not exist, create dir1, then create dir2
SFXDirectory::Create(SFXPath("/dir1/dir2/"), true);
[Note] Note

SFXPath is used to pass the file path argument to the function. (Reference: File Path Class).

Example 15.11. Delete the directory

// delete dir1 if it is empty
SFXDirectory::Remove(SFXPath("/dir1/"));

// recursively delete dir1 
SFXDirectory::Remove(SFXPath("/dir1/"), false);

Example 15.12. Check whether or not the directory exists

Bool b;

// return value of the Exists function is of the SFCError type
// b: set by Exists function whether or not the specifiled directory exists
SFXDirectory::Exists(SFXPath("/dir1/"), &b);

if (b) {
    // when directory exists

}

Example 15.13. Get the directory creation date

SFXDate date; // date

// date: directory creation date set with the GetCreateDate function
SFXDirectory::GetCreateDate(SFXPath("/dir1/"), &date);

Example 15.14. Get the device capacity and free space

UInt32 space1;
UInt32 space2;

SFXDirectory::DeviceTotalSpace(&space1); // space1: capacity set by DeviceTotalSpace function

SFXDirectory::DeviceFreeSpace(&space2);  // space2: free space set by DeviceFreeSpace function

Example 15.15. Check whether or not the directory is read only

Bool b;

// return value of the IsReadOnly function is of the SFCError type
// b: set by IsReadOnly function whether or not the specifiled directory is ReadOnly
SFXDirectory::IsReadOnly(SFXPath("/dir1/data.txt"), &b);

if (b) {
    // when directory is read only

}

Example 15.16. Get the temporary directory path

SFXPath path;
SFXPath parentPath;

path.Set(SFXAnsiString("/"));

SFXDirectory::GetTemporaryPath(parentPath, &path);
// directory path like "/sfx7182CBD4dir/" will be created
[Note] Note

The directory path that does not duplicate existing ones will be created for a temporary directory.

Example 15.17. Get the unique directory path

SFXPath path;
SFXPath parentPath;

path.Set(SFXAnsiString("/"));

SFXDirectory::GetUniquePath(parentPath, "data", "x", &path);
// directory path like "/data7182CBD4x/" will be created
[Note] Note

The directory path that does not duplicate existing ones will be created.

To enumerate files contained in the directory, use the file enumerator.

Example 15.18. Enumerate files contained in the specified directory

SFXPath path;
SFXDirectory::Enumerator etor;  // enumerator

// get the file enumerator
SFXDirectory::GetFileEnumerator(SFXPath("/dir1/"), &etor);

// enumerate files using the file enumerator
while (etor.HasNext()) {
    path = etor.GetNext();                       // get the file path in "/dir1"
    TRACE("file = %s", path.Get().GetCString()); // display the file path
}

To enumerate sub-directories contained in the directory, use the directory enumerator.

Example 15.19. Enumerate sub-directories contained in the specified directory

SFXPath path;
SFXDirectory::Enumerator etor;  // directory enumerator

// get directory enumerator
SFXDirectory::GetDirectoryEnumerator(SFXPath("/dir1/"), &etor);

// enumerate sub-directories using the directory enumerator
while (etor.HasNext()) {
    path = etor.GetNext();                      // get the directory path in "/dir1"
    TRACE("dir = %s", path.Get().GetCString()); // display the directory path
}
[Note] Note
To enumerate all the files and sub-directories contained in the specified directory recursively, to define the following recursive function.

Example 15.20. Enumerate all the files and sub-directories contained in the specified directory recursively

Void EnumerateDirectory(SFXPath path)
{
    SFXDirectory::Enumerator etor;

    // get the file enumerator
    SFXDirectory::GetFileEnumerator(path, &etor);

    while (etor.HasNext()) { // for each file
        SFXPath filepath = etor.GetNext();
        TRACE("file = %s", filepath.Get().GetCString()); // display the file path
    }

    // get the directory enumerator
    SFXDirectory::GetDirectoryEnumerator(path, &etor);

    while (etor.HasNext()) { // for each sub-directory
        SFXPath dirpath = etor.GetNext();
        TRACE("dir = %s", dirpath.Get().GetCString());   // display the directory path
        EnumerateDirectory(dirpath);
    }
}

Example 15.21. Method to use the EnumerateDirectory function

EnumerateDirectory(SFXPath("/dir1/"));  // list all files and directories under dir1