Utilities
Utilities header only library
fs Namespace Reference

An essential interface for manipulation with files and directories. Wraps file managing system calls. More...

Data Structures

struct  FileDirContent
 Information about single system directory entry. More...
 

Functions

bool FileExists (const char *filename)
 Checks if the specified file exist.
 
bool FileExists (const std::string &filename)
 Checks if the specified file exist.
 
bool FileCopy (const std::string &src, const std::string &dest)
 Makes a copy of a source file to a destination file. Destination directory must exist.
 
bool FileRename (const std::string &src_file, const std::string &new_name)
 Changes name of a source file to a new name.
 
bool FileRemove (const std::string &file)
 Deletes a specified file from the system. Deleted file will not appear in recycle bin.
 
bool DirectoryExists (const std::string &dir_path)
 Checks whether a specified directory exists. May contain the last slash but is not required.
 
bool CreateDir (const std::string &dir_path)
 Creates a specified directory. More...
 
bool CreateDirHierarchy (const std::string &dir_path)
 Creates a specified directory hierarchy. More...
 
bool RemoveDir (const std::string &dir_path, bool recursive=true)
 Removes input directory if it is empty or if recursive is set to true.
 
std::string GetExtension (const std::string &filename)
 Returns string representing file extension, in lower case with no dot.
 
bool HasExtenstion (const std::string &filename, const std::string &extenstion)
 Checks whether the file has a specific extension. An extension parameter is case sensitive and can be specified with or without a dot.
 
bool IsFileType (const std::string &filename, const file_types::Type type)
 Checks whether the file is of a specified type.
 
bool IsFileType (const std::string &filename, const std::vector< file_types::Type > &types)
 Checks whether the file is any of the specified types.
 
std::string FileToString (const std::string &file_name)
 Loads contents of a text-based file and stores it in a string.
 
void StringToFile (const std::string &file_name, const std::string &str)
 Exports the string to a text file.
 
template<typename T >
std::vector< T > FileToVector (const std::string &file_name)
 Loads contents of a text-based file and stores it in a vector. The file must contain only elements of templated type.
 
std::vector< FileDirContent > ListDirectoryContents (std::string direcory_path, const bool recursive=false)
 Returns list of all contents in a specified directory.
 
std::vector< std::string > ListDirectoryFileNames (const std::string &direcory_path, const std::string &extension="")
 Returns list of all file contents in a specified directory (ignores directories). Providing extension parameter returns only files with specified extension.
 
std::vector< std::string > ListDirectoryFileNames (const std::string &direcory_path, const file_types::Type file_type)
 Returns a list of all file contents in a specified directory (ignores directories) with extension of the specified file type.
 
std::vector< std::string > ListDirectoryFileNames (const std::string &direcory_path, const std::vector< file_types::Type > &file_types)
 Returns a list of all file contents in a specified directory (ignores directories) with any of the extensions of the specified file types.
 
bool DirectoryContainsAny (const std::string &direcory_path, const file_types::Type file_type)
 Checks specified directory if it contains file(s) of specified type.
 
bool DirectoryContainsAny (const std::string &direcory_path, const std::vector< file_types::Type > &file_types)
 Checks specified directory if it contains file(s) of any of the specified types.
 
std::string GetDirectoryPathOfFile (const std::string &file_name_with_path)
 Takes specified file path and strips everything after the last directory separator (including the separator).
 
std::string GetFileName (const std::string &file_name_with_path, bool strip_extension=false)
 Strips everything before the last directory separator, including the separator. If parameter strip_extension is true, strips also extension from resulting file name.
 
std::string GetAbsolutePath (const std::string &local_path)
 Retrieves full system path to a specified local file or directory. Specified local path may contain non existing directories and files.
 
std::string Execute (const char *command, const size_t buffer_size=128)
 Executes command as if it was called from the command line. More...
 
int RunExe (const std::string &exe_path, const std::string &cmd_arguments="")
 Runs executable in command line.
 
std::string StripExtension (const std::string &file_name_with_extension)
 Strips the extension from a file name. More...
 
bool FileExistsInDirectory (const std::string &dir_path, const std::string &filename, bool strip_extension=false)
 Checks whether a specified file exists in directory. More...
 

Detailed Description

An essential interface for manipulation with files and directories. Wraps file managing system calls.

Function Documentation

◆ CreateDir()

bool fs::CreateDir ( const std::string &  dir_path)
inline

Creates a specified directory.

This function does not create directory hierarchies. If you wish to create hierarchy, use CreateDirHierarchy function instead.

Parameters
dir_pathSpecifies path and name of directory. May contain the last slash but is not required.

◆ CreateDirHierarchy()

bool fs::CreateDirHierarchy ( const std::string &  dir_path)
inline

Creates a specified directory hierarchy.

This function creates directory hierarchies. If parent folder does not exists, function creates it recursively.

Parameters
file_pathSpecifies path and name of directory. May contain the last slash but is not required.

◆ Execute()

std::string fs::Execute ( const char *  command,
const size_t  buffer_size = 128 
)
inline

Executes command as if it was called from the command line.

Parameters
commandCommand to be called.
buffer_sizeDetermines size of temporal buffer used to obtain output.
Returns
Standard output generated by the executed command.

◆ FileExistsInDirectory()

bool fs::FileExistsInDirectory ( const std::string &  dir_path,
const std::string &  filename,
bool  strip_extension = false 
)
inline

Checks whether a specified file exists in directory.

Parameters
dir_pathSpecifies path and name of directory. May contain the last slash but is not required.
filenameSpecifies name of file. May contain the extension.
strip_extensionIf it is true, strips also extension from file name. Default is false.
Here is the call graph for this function:

◆ StripExtension()

std::string fs::StripExtension ( const std::string &  file_name_with_extension)
inline

Strips the extension from a file name.

Parameters
file_name_with_extensionSpecifies name of the file.
Here is the caller graph for this function: