BrowseFiles Manual

About

The BrowseFiles class provides means to iterate through the content of a directory.

Using the BrowseFiles class always follows the same concept:

  • Allocate a BrowseFiles instance.
  • Initalizes the BrowseFiles instance via Init().
  • Call GetNext() at least once (but most likely in a loop) to get the first/next element (if any).
  • Use the below mentioned "File Info" functions on each element.
// This example demonstrates browsing a directory's content with BrowseFiles.
// Let user select an arbitrary directory to browse.
if (!folderName.FileSelect(FILESELECTTYPE_ANYTHING, FILESELECT_DIRECTORY, "Select a directory..."))
return false;
// Allocate a BrowseFiles instance.
if (!bf)
{
GePrint("Error: Failed to allocate BrowseFiles instance");
return false;
}
// Initialize the BrowseFiles instance.
const Int32 bfFlags = BROWSEFILES_CALCSIZE;
bf->Init(folderName, bfFlags);
// Print the directory content.
GePrint("Content of " + folderName.GetString());
while (bf->GetNext()) // Note: GetNext() needs to be called at least once, to get the first element.
{
String sIsBundle = "_", sIsHidden = "_", sIsReadOnly = "_", sIsDir = "_";
String sSize = "dir";
LocalFileTime timeCreated;
String sTimeCreated;
String sAttr;
// Gather file information flags.
if (bf->IsBundle())
sIsBundle = "b";
if (bf->IsHidden())
sIsHidden = "h";
if (bf->IsReadOnly())
sIsReadOnly = "r";
if (bf->IsDir())
sIsDir = "d";
else
sSize = String::MemoryToString(bf->GetSize()); // GetSize() is only valid for files.
// Get creation time and convert it into a string.
bf->GetFileTime(GE_FILETIME_CREATED, &timeCreated); // other options: GE_FILETIME_MODIFIED and GE_FILETIME_ACCESS
sTimeCreated = String::IntToString(timeCreated.year);
sTimeCreated += String("-") + String::IntToString(timeCreated.month);
sTimeCreated += String("-") + String::IntToString(timeCreated.day);
sTimeCreated += String(" ") + String::IntToString(timeCreated.hour);
sTimeCreated += String(":") + String::IntToString(timeCreated.minute);
sTimeCreated += String(":") + String::IntToString(timeCreated.second);
// Call a small custom function to get a string with decoded file attributes, see File Functions Manual.
FileGetAttributesString(folderName + bf->GetFilename(), sAttr); // Note: Pass complete path to file here!
// Finally print all information in one line.
GePrint("[" + sIsDir + sIsBundle + sIsHidden + sIsReadOnly + "] - " + sSize + " - " + sTimeCreated + " - " + sAttr + " - " + bf->GetFilename().GetFileString());
}

Allocation/Deallocation

BrowseFiles objects are created with the usual tools, see Entity Creation and Destruction Manual.

Use

Browse

Note
BrowseFiles::GetNext() needs to be called at least once, in order to get the first element and for the following "File Info" functions to work.

File Info

Further Reading