PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

15.3. Class for Operating a Directory

SFXDirectory is the class for operating a directory such as creating, moving, deleting, retrieving, traversing the subdirectories 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. (Related information: 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 the directory exists or not

Bool b;

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

if (b) {
    // when directory exists

}

Example 15.13. Get the directory creation date

SFXDate date; // date class

// date: directory creation date set by 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 the directory is read only or not

Bool b;

// return value of IsReadOnly function is of SFCError type
// b: set by IsReadOnly function whether specifiled directory is ReadOnly or not
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 file enumerator
SFXDirectory::GetFileEnumerator(SFXPath("/dir1/"), &etor);

// enumerate files using file enumerator
while (etor.HasNext()) {
    path = etor.GetNext();                       // get file path in "/dir1"
    TRACE("file = %s", path.Get().GetCString()); // display 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 directory enumerator
while (etor.HasNext()) {
    path = etor.GetNext();                      // get directory path in "/dir1"
    TRACE("dir = %s", path.Get().GetCString()); // display 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 file enumerator
    SFXDirectory::GetFileEnumerator(path, &etor);

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

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

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

Example 15.21. Method to use the EnumerateDirectory function

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