File Functions Manual

About

Cinema 4D's API provides multiple utility functions to work with files.

// Small helper function demonstrating ShowInFinder().
static void InfoStop(const Filename& fn, const String& msg)
{
ShowInFinder(fn, false);
}
// A useless example to demonstrate a bunch of file functions.
// First get the desktop directory, this example will search for a testdocument.txt below the desktop.
Filename fnSrc;
// Search for a test file.
if (!GeSearchFile(fnDesktop, "testdocument.txt", &fnSrc))
{
GePrint("Error: Didn't find a file called \"testdocument.txt\" below the desktop directory.");
GePrint(" Please create one for this demo to work.");
return false;
}
// Check if file exists (well, in this case it always does, as GeSearchFile() returned true).
if (!GeFExist(fnSrc, false))
{
GePrint("Error: The selected file (" + fnSrc.GetString() + ") doesn't exist.");
return false;
}
// Create a few sub-directories in the directory containing the "test file".
Filename fnNewDir = fnSrc.GetDirectory();
fnNewDir += "myDir";
fnNewDir += "mySubDir1";
fnNewDir += "mySubDir2";
if (!GeFCreateDirRec(fnNewDir)) // Note: GeFCreateDirRec() has to be used if several sub-directories are to be created in one step.
{
GePrint("Error: Failed to create directory: " + fnNewDir.GetString());
return false;
}
InfoStop(fnNewDir, "New directory successfully created, watch opened Explorer/Finder"); // small helper function
// Copy the file into the sub-directory.
const Filename fnDest = fnNewDir + fnSrc.GetFile();
if (!GeFCopyFile(fnSrc, fnDest, 0))
{
GePrint("Error: Failed to copy file (" + fnSrc.GetString() + ") to " + fnDest.GetString());
return false;
}
InfoStop(fnDest, "Source file successfully copied to new directory, watch opened Explorer/Finder"); // small helper function
// Rename the copy.
Filename fnDestRenamed = fnDest;
fnDestRenamed.SetFile("newName.test");
if (!GeFRename(fnDest, fnDestRenamed))
{
GePrint("Error: Failed to rename file (" + fnDest.GetString() + ") to " + fnDestRenamed.GetString());
return false;
}
InfoStop(fnDestRenamed, "File successfully renamed, watch opened Explorer/Finder"); // small helper function
// Move the renamed copy.
Filename fnDestMoved = fnSrc.GetDirectory();
fnDestMoved += "myDir";
fnDestMoved += fnDestRenamed.GetFile();
if (!GeFMove(fnDestRenamed, fnDestMoved))
{
GePrint("Error: Failed to move file (" + fnDestRenamed.GetString() + ") to " + fnDestMoved.GetString());
return false;
}
InfoStop(fnDestMoved, "File successfully moved, watch opened Explorer/Finder"); // small helper function
// Finally, remove the file and all created directories again.
Filename fnDelDir = fnSrc.GetDirectory();
fnDelDir += "myDir";
{
GePrint("Error: Failed to delete created directories and files (" + fnDelDir.GetString() + ")");
return false;
}
InfoStop(fnDestMoved, "Successfully deleted all directories/files created in this test, watch opened Explorer/Finder"); // small helper function

File Functions

All the usual functions to work with files (copy, rename, delete,...) are available in Cinema 4D's API:

Note
If a new file needs to be created from scratch, please see BaseFile Manual or HyperFile Manual.
Warning
Beware, GeFKill() deletes files permanently. The files will not be moved to system's trash can.

Folders/Directories

Creation

Default Paths

File Attributes

File Attribute Flags:

// Small helper function to decode file attributes into a string.
static void FileGetAttributesString(const Filename& fn, String &sAttr)
{
const UInt32 attr = GeFGetAttributes(fn);
sAttr = "";
// Note: Depending on the platform (Win/OSX) not all flags make sense.
sAttr += "RO_";
else
sAttr += "RW_";
sAttr += "H_";
else
sAttr += "__";
sAttr += "L_";
else
sAttr += "__";
sAttr += "r";
else
sAttr += "_";
sAttr += "w";
else
sAttr += "_";
sAttr += "x";
else
sAttr += "_";
sAttr += "r";
else
sAttr += "_";
sAttr += "w";
else
sAttr += "_";
sAttr += "x";
else
sAttr += "_";
sAttr += "r";
else
sAttr += "_";
sAttr += "w";
else
sAttr += "_";
sAttr += "x";
else
sAttr += "_";
return;
}

File Execution

  • GeExecuteFile(): Opens the file as if the user double-clicked it. The default application for this file will open.
  • GeExecuteProgram(): Asynchronously executes an application.
  • GeOpenHTML(): Opens an URL in the user's default web browser.

Volumes/Drives

File Type Identification

Documents

BaseDocument class contains functions to load documents from files (LoadDocument() and LoadFile()). See Disc I/O in the BaseDocument Manual for more information.

Images

BaseBitmap class contains functions to read/write images from/to files (BaseBitmap::Init() and BaseBitmap::Save()). Images can also be read/written from/to a HyperFile (HyperFile::ReadImage() and HyperFile::WriteImage()). See Disc I/O in the BaseBitmap Manual and BaseBitmap in HyperFile Manual for more information.

Sounds

Sound files can be loaded and saved with the respective functions (BaseSound::Load() and BaseSound::Save()) of the BaseSound class.

Miscellaneous

  • ShowInFinder(): Shows the file/path in the Finder (Mac) or Explorer (Windows).

Further Reading