Directories, files, and file names

Header: #include <mrpt/system/filesystem.h>.

Library: [mrpt-system]

// global functions

std::string mrpt::system::getTempFileName();
std::string mrpt::system::getcwd();
std::string mrpt::system::getShareMRPTDir();
bool mrpt::system::createDirectory(const std::string& dirName);
bool mrpt::system::deleteFile(const std::string& fileName);
void mrpt::system::deleteFiles(const std::string& s);

bool mrpt::system::renameFile(
    const std::string& oldFileName,
    const std::string& newFileName,
    std::string* error_msg = nullptr
    );

bool mrpt::system::deleteFilesInDirectory(const std::string& s, bool deleteDirectoryAsWell = false);
std::string mrpt::system::extractFileName(const std::string& filePath);
std::string mrpt::system::extractFileExtension(const std::string& filePath, bool ignore_gz = false);
std::string mrpt::system::extractFileDirectory(const std::string& filePath);
bool mrpt::system::fileExists(const std::string& fileName);
bool mrpt::system::directoryExists(const std::string& fileName);

std::string mrpt::system::fileNameStripInvalidChars(
    const std::string& filename,
    const char replacement_to_invalid_chars = '_'
    );

std::string mrpt::system::fileNameChangeExtension(const std::string& filename, const std::string& newExtension);
uint64_t mrpt::system::getFileSize(const std::string& fileName);
time_t mrpt::system::getFileModificationTime(const std::string& filename);
std::string mrpt::system::filePathSeparatorsToNative(const std::string& filePath);

bool mrpt::system::copyFile(
    const std::string& sourceFile,
    const std::string& targetFile,
    std::string* outErrStr = nullptr
    );

std::string mrpt::system::toAbsolutePath(const std::string& path, bool resolveToCanonical = false);
std::string mrpt::system::pathJoin(const std::vector<std::string>& paths);

// macros

#define ASSERT_DIRECTORY_EXISTS_(DIR)
#define ASSERT_FILE_EXISTS_(FIL)

Global Functions

std::string mrpt::system::getTempFileName()

Returns the name of a proposed temporary file name.

std::string mrpt::system::getcwd()

Returns the current working directory.

std::string mrpt::system::getShareMRPTDir()

Attempts to find the directory [PREFIX/]share/mrpt/ and returns its absolute path, or empty string if not found.

Example return paths: Linux after installing = /usr/share/mrpt/; manually-built system = [MRPT_SOURCE_DIR]/share/mrpt/, etc.

bool mrpt::system::createDirectory(const std::string& dirName)

Creates a directory.

Returns:

Returns false on any error, true on directory created or already existed.

bool mrpt::system::deleteFile(const std::string& fileName)

Deletes a single file.

For multiple files see deleteFiles

Returns:

Returns false on any error, true on everything OK.

See also:

deleteFiles

void mrpt::system::deleteFiles(const std::string& s)

Delete one or more files, especified by the (optional) path and the file name (including ‘?’ or ‘*’) - Use forward slash (‘/’) for directories for compatibility between Windows and Linux, since they will be internally traslated into backward slashes (’') if MRPT is compiled under Windows.

See also:

deleteFile

bool mrpt::system::renameFile(
    const std::string& oldFileName,
    const std::string& newFileName,
    std::string* error_msg = nullptr
    )

Renames a file - If the target path is different and the filesystem allows it, it will be moved to the new location.

Returns:

false on any error. In that case, if a pointer to a receiver string is passed in error_msg, a description of the error is saved there.

bool mrpt::system::deleteFilesInDirectory(
    const std::string& s,
    bool deleteDirectoryAsWell = false
    )

Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).

Returns:

true on success

See also:

deleteFile

std::string mrpt::system::extractFileName(const std::string& filePath)

Extract just the name (without extension) of a filename from a complete path plus name plus extension.

This function works for either “/” or “" directory separators.

See also:

extractFileExtension, extractFileDirectory

std::string mrpt::system::extractFileExtension(
    const std::string& filePath,
    bool ignore_gz = false
    )

Extract the extension of a filename.

For example, for “dummy.cpp”, it will return “cpp”. If “ignore_gz” is true, the second extension will be returned if the file name ends in “.gz”, for example, for “foo.map.gz”, this will return “map”.

See also:

extractFileName, extractFileDirectory

std::string mrpt::system::extractFileDirectory(const std::string& filePath)

Extract the whole path (the directory) of a filename from a complete path plus name plus extension.

This function works for either “/” or “" directory separators.

See also:

extractFileName, extractFileExtension

bool mrpt::system::fileExists(const std::string& fileName)

Test if a given file (or directory) exists.

See also:

directoryExists

bool mrpt::system::directoryExists(const std::string& fileName)

Test if a given directory exists (it fails if the given path refers to an existing file).

See also:

fileExists

std::string mrpt::system::fileNameStripInvalidChars(
    const std::string& filename,
    const char replacement_to_invalid_chars = '_'
    )

Replace invalid filename chars by underscores (’_’) or any other user-given char.

Invalid chars are: ‘<’,’>’,’:’,’”’,’/’,’',’|’,’?’,’*’

std::string mrpt::system::fileNameChangeExtension(
    const std::string& filename,
    const std::string& newExtension
    )

Replace the filename extension by another one.

Example:

fileNameChangeExtension("cool.txt","bar") // -> "cool.bar"
uint64_t mrpt::system::getFileSize(const std::string& fileName)

Return the size of the given file, or size_t(-1) if some error is found accessing that file.

time_t mrpt::system::getFileModificationTime(const std::string& filename)

Return the time of the file last modification, or “0” if the file doesn’t exist.

std::string mrpt::system::filePathSeparatorsToNative(const std::string& filePath)

Windows: replace all ‘/’->’' , in Linux/MacOS: replace all ‘'->’/’.

bool mrpt::system::copyFile(
    const std::string& sourceFile,
    const std::string& targetFile,
    std::string* outErrStr = nullptr
    )

Copies file sourceFile to targetFile.

If the target file exists, it will be overwritten.

(In MRPT 2.5.0, the copyAttributes param was removed)

Returns:

true on success, false on any error, whose description can be optionally get in outErrStr

std::string mrpt::system::toAbsolutePath(
    const std::string& path,
    bool resolveToCanonical = false
    )

Portable version of std::filesystem::absolute() and canonical()

If canonical==true relative paths, symlinks, etc. will be resolved too, but an exception will be thrown if the referenced file/path does not exist. If canonical==true, an absolute path will be always returned, even if does not actually exist.

mrpt::system::toAbsolutePath("/home/joe"); // -> "/home/joe"
mrpt::system::toAbsolutePath("../mary"); // -> "/home/mary"

(New in MRPT 2.5.0)

std::string mrpt::system::pathJoin(const std::vector<std::string>& paths)

Portable version of std::filesystem::path::append(), with Python-like name.

mrpt::system::pathJoin({"/home", "foo"}); // -> "/home/foo"
mrpt::system::pathJoin({"/home", "/tmp/a"}); // -> "/tmp/a"
mrpt::system::pathJoin({".", "sub","file.txt"}); // -> "./sub/file.txt"

(New in MRPT 2.5.0)