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.
  • Initializes the BrowseFiles instance via BrowseFiles::Init().
  • Call BrowseFiles::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.
Note
For browsing files with the Maxon API see Url Manual.
// 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..."_s))
return maxon::OK;
// Allocate a BrowseFiles instance.
AutoAlloc<BrowseFiles> bf;
if (bf == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
// Initialize the BrowseFiles instance.
const Int32 bfFlags = BROWSEFILES_CALCSIZE;
bf->Init(folderName, bfFlags);
// Print the directory content.
ApplicationOutput("Content of @", maxon::String(folderName.GetString()));
while (bf->GetNext()) // Note: GetNext() needs to be called at least once, to get the first element.
{
maxon::String sIsBundle = "_"_s, sIsHidden = "_"_s, sIsReadOnly = "_"_s, sIsDir = "_"_s;
maxon::String sSize = "dir"_s;
LocalFileTime timeCreated;
maxon::String sTimeCreated;
// Gather file information flags.
if (bf->IsBundle())
sIsBundle = "b"_s;
if (bf->IsHidden())
sIsHidden = "h"_s;
if (bf->IsReadOnly())
sIsReadOnly = "r"_s;
if (bf->IsDir())
sIsDir = "d"_s;
else
sSize = maxon::String::MemorySizeToString(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 = maxon::String::IntToString(timeCreated.year);
sTimeCreated += "-"_s + maxon::String::IntToString(timeCreated.month);
sTimeCreated += "-"_s + maxon::String::IntToString(timeCreated.day);
sTimeCreated += " "_s + maxon::String::IntToString(timeCreated.hour);
sTimeCreated += ":"_s + maxon::String::IntToString(timeCreated.minute);
sTimeCreated += ":"_s + maxon::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) iferr_return; // Note: Pass complete path to file here!
// Finally print all information in one line.
const maxon::String fileName = maxon::String(bf->GetFilename().GetFileString());
ApplicationOutput("[@ @ @ @] - @ - @ - @ - @"_s, sIsDir, sIsBundle, sIsHidden, sIsReadOnly, sSize, sTimeCreated, sAttr, fileName);
}
Definition: string.h:1287
DIRECTORY
Folder selection dialog.
Definition: ge_prepass.h:2
ANYTHING
Any file.
Definition: ge_prepass.h:0
#define BROWSEFILES_CALCSIZE
Specifies if BrowseFiles::GetSize() can be called later on. Only works for files, not for folders.
Definition: c4d_file.h:914
return OK
Definition: apibase.h:2740
#define GE_FILETIME_CREATED
File time created.
Definition: c4d_file.h:906
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
maxon::Int32 Int32
Definition: ge_sys_math.h:51
#define iferr_return
Definition: resultbase.h:1531

Allocation/Deallocation

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

  • BrowseFiles::Alloc(): Allocates a BrowseFiles instance.
  • BrowseFiles::Free(): Destroys a BrowseFiles instance.

Use

Browse

  • BrowseFiles::Init(): Initializes the class to browse a certain directory.
    • Parameter "flags":
      • BROWSEFILES_CALCSIZE: Specifies if BrowseFiles::GetSize() can be called later on. Only works for files, not for folders.
      • ::BROWSEFILES_SUPPRESSCACHING: Suppress caching.
  • BrowseFiles::GetNext(): Gets the first or next file or subdirectory in the browsed directory.
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

  • BrowseFiles::GetFilename(): Gets the name of the current file or directory.
  • BrowseFiles::GetSize(): Gets the size for the current file.
  • BrowseFiles::GetFileTime(): Gets a time (::LocalFileTime) for the current file or directory.
  • BrowseFiles::IsDir(): Checks if the current element is a directory.
  • BrowseFiles::IsReadOnly(): Checks if the current file or directory is read-only.
  • BrowseFiles::IsHidden(): Checks if the current file or directory is hidden.
  • BrowseFiles::IsBundle(): Checks if the current file or directory is a bundle.

Further Reading