Skip to main content

FileUtils

#include <file_utils.hpp>

Public Static Methods

ReturnNameDescription
booldirectory_exists staticCheck if directory exists.
boolfile_exists staticCheck if file exists.
boolpath_exists staticCheck if path exists (file or directory)
Result< void >create_directory staticCreate directory (and parent directories if needed)
Result< void >copy_directory staticCopy directory (recursive) Uses hard links when possible for efficiency (like Typesense)
Result< void >move_path staticMove/rename path (works for files and directories)
Result< void >delete_path staticDelete path (file or directory)
Result< size_t >file_size staticGet file size in bytes.
Result< std::vector< std::string > >list_files staticList files in directory.
Result< std::string >read_file staticRead entire file into string.
Result< std::vector< std::string > >read_lines staticRead file into vector of lines.
Result< void >write_file staticWrite string to file (overwrites existing)
Result< void >append_file staticAppend string to file.
std::stringget_extension staticGet file extension (without dot)
std::stringget_filename staticGet filename without directory.
std::stringget_directory staticGet directory path.
Result< std::string >absolute_path staticGet absolute path.
Result< std::string >current_directory staticGet current working directory.

directory_exists

static
static inline bool directory_exists(const std::string & dir_path) noexcept
Check if directory exists.

file_exists

static
static inline bool file_exists(const std::string & file_path) noexcept
Check if file exists.

path_exists

static
static inline bool path_exists(const std::string & path) noexcept
Check if path exists (file or directory)

create_directory

static
static inline Result< void > create_directory(const std::string & dir_path)
Create directory (and parent directories if needed)

copy_directory

static
static inline Result< void > copy_directory(const std::string & from_path, const std::string & to_path)
Copy directory (recursive) Uses hard links when possible for efficiency (like Typesense)

move_path

static
static inline Result< void > move_path(const std::string & from_path, const std::string & to_path)
Move/rename path (works for files and directories)

delete_path

static
static inline Result< void > delete_path(const std::string & path, bool recursive)
Delete path (file or directory)

Parameters

  • recursive If true, delete directory contents recursively

file_size

static
static inline Result< size_t > file_size(const std::string & file_path)
Get file size in bytes.

list_files

static
static inline Result< std::vector< std::string > > list_files(const std::string & dir_path, const std::string & pattern)
List files in directory.

Parameters

  • pattern Optional glob pattern (e.g., “*.txt”)

read_file

static
static inline Result< std::string > read_file(const std::string & file_path)
Read entire file into string.

read_lines

static
static inline Result< std::vector< std::string > > read_lines(const std::string & file_path)
Read file into vector of lines.

write_file

static
static inline Result< void > write_file(const std::string & file_path, const std::string & content)
Write string to file (overwrites existing)

append_file

static
static inline Result< void > append_file(const std::string & file_path, const std::string & content)
Append string to file.

get_extension

static
static inline std::string get_extension(const std::string & file_path)
Get file extension (without dot)

get_filename

static
static inline std::string get_filename(const std::string & file_path)
Get filename without directory.

get_directory

static
static inline std::string get_directory(const std::string & file_path)
Get directory path.

absolute_path

static
static inline Result< std::string > absolute_path(const std::string & path)
Get absolute path.

current_directory

static
static inline Result< std::string > current_directory()
Get current working directory.