Author: Amin Tahmasebi Release Date: 2024 License: ISC License
This library is dedicated to directory and file operations, offering comprehensive functionality to manage and manipulate files and directories on the filesystem. It is designed to provide a straightforward and efficient way to interact with the filesystem in C programming.
- Directory Operations: Create, delete, rename, and navigate through directories.
- File Operations: Handle file creation, deletion, renaming, encryption, decryption, and more.
- Directory Listing: Retrieve lists of files and directories, including recursive searches.
- File Information: Obtain details about files and directories, such as size, type, owner, and timestamps.
- Path Manipulation: Work with absolute and relative paths, and convert between them.
- File and Directory Search: Search for files and directories based on patterns and conditions.
- Error Handling: Includes robust error handling for reliable file system operations.
- Cross Platform: Work in Windows and linux.
- Support Unicode: work with differnt kind of name file.
-
Purpose: This function attempts to create a directory at the specified path. It handles directory creation differently based on the operating system (Windows or Unix-like systems).
-
Parameters:
dirpath
: A pointer to a string representing the path of the directory to be created.
-
Return Value:
true
: If the directory is successfully created.false
: If the directory creation fails for any reason, such as invalid permissions or a null directory path.
-
Input Validation:
- The function first checks if the provided
dirpath
isNULL
. If it is, the function logs an error message and returnsfalse
.
- The function first checks if the provided
- Purpose: This function retrieves the name of the directory from a given directory path. If the input is the current directory (
"."
), it will return the name of the current working directory. - Parameters:
dirpath
: A pointer to a string representing the directory path.
- Return Value:
- A dynamically allocated string containing the directory name, or an empty string if an error occurs. The caller is responsible for freeing this memory.
- Input Validation:
- The function first checks if the provided
dirpath
isNULL
. If so, it logs an error and returnsNULL
.
- The function first checks if the provided
- Purpose: This function retrieves the current working directory's path.
- Parameters: None.
- Return Value:
- A dynamically allocated string containing the current working directory, or
NULL
on error. The caller is responsible for freeing this memory.
- A dynamically allocated string containing the current working directory, or
-
Purpose: This function counts the number of items (files and directories) in a specified directory.
-
Parameters:
dirpath
: A string representing the path to the directory.
-
Return Value:
- Returns the number of items in the directory, or
-1
if an error occurs (e.g., if the directory cannot be opened).
- Returns the number of items in the directory, or
-
Input Validation:
- The function first checks if the
dirpath
isNULL
. If so, it logs an error and returns-1
.
- The function first checks if the
- Purpose: This function converts a relative file path to an absolute file path.
- Parameters:
relative_path
: A string representing the relative file path.
- Return Value:
- Returns a dynamically allocated string containing the absolute file path, or
NULL
if an error occurs.
- Returns a dynamically allocated string containing the absolute file path, or
- Input Validation:
- The function checks if the
relative_path
isNULL
. If so, it logs an error and returnsNULL
.
- The function checks if the
-
Purpose: This function changes the current working directory to the specified directory.
-
Parameters:
dirName
: A string representing the name of the directory to change to.
-
Return Value:
- Returns
true
if the directory was successfully changed, orfalse
if an error occurs.
- Returns
-
Input Validation:
- The function checks if the
dirName
isNULL
. If so, it logs an error and returnsfalse
.
- The function checks if the
-
Purpose: This function changes the current working directory to the parent directory (i.e., the directory one level up from the current directory).
-
Parameters: None.
-
Return Value:
- Returns
true
if the directory change was successful, orfalse
otherwise.
- Returns
-
Logging: The function logs that it is changing the directory to the parent (
..
). -
Calling
dir_cd
: It calls thedir_cd
function with".."
as the argument, which changes the directory to the parent directory. -
Return: It returns the result of the
dir_cd
function.
-
Purpose: This function removes a directory if it is empty.
-
Parameters:
dirName
: A string representing the name or path of the directory to remove.
-
Return Value:
- Returns
true
if the directory was successfully removed, orfalse
if the directory is not empty or an error occurred.
- Returns
-
Input Validation: It first checks if the directory name (
dirName
) isNULL
. If so, it logs an error and returnsfalse
. -
Checking Directory Contents: It calls the
dir_is_empty
function to check if the directory is empty. If the directory is not empty, it logs an error and returnsfalse
.
- Purpose: This function checks if a directory is empty.
- Parameters:
dirName
: A string representing the name or path of the directory to check.
- Return Value:
- Returns
true
if the directory is empty,false
if it is not empty or if an error occurs.
- Returns
- Input Validation: It checks if the directory name is
NULL
. If so, it logs an error and returnsfalse
.
-
Purpose: This function recursively removes a directory and all its contents.
-
Parameters:
dirPath
: A string representing the path of the directory to be removed.
-
Return Value:
- Returns
true
if the directory and its contents were successfully removed, orfalse
if an error occurred.
- Returns
-
Input Validation: It checks if the directory path is
NULL
. If so, it logs an error and returnsfalse
.
-
Purpose: This function renames a file or directory from
oldName
tonewName
. -
Parameters:
oldName
: A string representing the current name of the file or directory.newName
: A string representing the new name for the file or directory.
-
Return Value:
- Returns
true
if the renaming was successful, orfalse
if it failed.
- Returns
-
Input Validation: The function first checks if both
oldName
andnewName
are provided. If either isNULL
, it logs an error and returnsfalse
.
-
Purpose: This function checks if a directory exists at the specified path.
-
Parameters:
dirPath
: A string representing the path of the directory to check.
-
Return Value:
- Returns
true
if the directory exists, orfalse
if it does not exist or if an error occurs.
- Returns
-
Input Validation: It checks if
dirPath
isNULL
. If so, it logs an error and returnsfalse
.
- Purpose: This function checks if a file exists at the specified path.
- Parameters:
filePath
: A string representing the path of the file to check.
- Return Value:
- Returns
true
if the file exists, orfalse
if it does not exist or if an error occurs.
- Returns
- Input Validation: It checks if
filePath
isNULL
. If so, it logs an error and returnsfalse
.
-
Purpose: This function copies a file from a source path (
srcPath
) to a destination path (destPath
). -
Parameters:
srcPath
: The path of the source file to be copied.destPath
: The destination file path where the file should be copied to.
-
Return Value:
- Returns
true
if the file was successfully copied, orfalse
if an error occurred.
- Returns
-
Input Validation: The function first checks if both
srcPath
anddestPath
are provided. If either isNULL
, it logs an error and returnsfalse
.
-
Purpose: This function copies an entire directory, including its contents (files and subdirectories), from a source path (
srcDir
) to a destination path (destDir
). -
Parameters:
srcDir
: The path of the source directory to be copied.destDir
: The path of the destination directory where the contents should be copied.
-
Return Value:
- Returns
true
if the directory and its contents were successfully copied, orfalse
if an error occurred.
- Returns
-
Input Validation: The function first checks if both
srcDir
anddestDir
are provided. If either isNULL
, it logs an error and returnsfalse
.
-
Purpose:
This function calculates the total size of a directory and all its contents, including subdirectories, in bytes. It provides different implementations based on the operating system (Windows or Unix-like systems). If any error occurs, such as aNULL
directory path or failure to compute the size, the function returns-1
. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory to be analyzed for its size.
-
Return Value:
- The size of the directory in bytes if successful.
-1
: If an error occurs (e.g., invalid directory path or failure in size calculation).
-
Input Validation:
The function first checks if the provideddirPath
isNULL
. If it is, the function logs an error message and returns-1
.
-
Purpose:
This function calculates the size of a specific file in bytes. It handles file size calculation differently depending on the operating system (Windows or Unix-like systems). If any error occurs, such as aNULL
file path or failure to calculate the size, the function returns-1
. -
Parameters:
filePath
: A pointer to a string representing the path of the file whose size is to be determined.
-
Return Value:
- The size of the file in bytes if successful.
-1
: If an error occurs (e.g., invalid file path or failure to calculate the file size).
-
Input Validation:
The function first checks if the providedfilePath
isNULL
. If it is, the function logs an error message and returns-1
.
-
Purpose: This function lists the contents of a directory based on the specified option (e.g., files only, directories only, or both). The results are stored in a
Vector
. -
Parameters:
dirPath
: The path of the directory to list.option
: Specifies what to list (e.g., files, directories, or both).resultVector
: A vector where the results will be stored.
-
Return Value:
- void: This function does not return any value. The results (directory contents) are instead stored in the resultVector. Errors are handled internally, typically through logging, rather than returning an error code.
- Purpose: This function checks if the specified path points to a regular file. It returns
true
if the path points to a file andfalse
if it does not exist or points to something other than a regular file (e.g., a directory, symbolic link, etc.). - Parameters:
filePath
: A pointer to a string that specifies the path of the file to check. The path must be a valid file system path. IffilePath
isNULL
, the function logs an error and returnsfalse
.
- Return Value:
true
: If the path points to a regular file.false
: If the path does not exist, points to something other than a regular file, or if there is an error (such as aNULL
path or file access issues).
- Error Handling: If the provided
filePath
isNULL
or the file does not exist, the function logs the error and returnsfalse
.
-
Purpose:
This function checks whether the specified path is a directory. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory to be checked.
-
Return Value:
true
: If the path is a directory.false
: If the path is not a directory, the directory does not exist, or an error occurs.
-
Input Validation:
The function first checks if the provideddirPath
isNULL
. If it is, the function logs an error message and returnsfalse
.
-
Purpose:
This function moves a file from a source path to a destination path. -
Parameters:
srcPath
: A pointer to a string representing the source file path.destPath
: A pointer to a string representing the destination file path.
-
Return Value:
true
: If the file was successfully moved.false
: If an error occurs (e.g.,NULL
paths, failure to move the file).
-
Input Validation:
The function checks if bothsrcPath
anddestPath
areNULL
. If either isNULL
, it logs an error message and returnsfalse
.
- Purpose:
This function moves a directory and its contents from a source path to a destination path. - Parameters:
srcPath
: A pointer to a string representing the source directory path.destPath
: A pointer to a string representing the destination directory path.
- Return Value:
true
: If the directory was successfully moved.false
: If an error occurs (e.g.,NULL
paths, failure to move the directory).
- Input Validation:
The function checks if bothsrcPath
anddestPath
areNULL
. If either isNULL
, it logs an error message and returnsfalse
.
-
Purpose:
This function retrieves the last modified time of a directory as a string. It supports both Windows and POSIX systems. The modified time is useful for tracking when changes were last made to the directory. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory.
-
Return Value:
- A dynamically allocated string representing the modified time of the directory in the format "YYYY-MM-DD HH:MM:SS".
NULL
: If an error occurs (e.g.,NULL
directory path or failure to retrieve the time).
-
Input Validation:
If thedirPath
isNULL
, the function logs an error and returnsNULL
. -
Error Handling:
If the directory path is invalid, or memory allocation fails, the function logs appropriate error messages and returnsNULL
.
-
Purpose:
This function retrieves the creation time of a directory as a string. Creation time retrieval is only supported on Windows systems. POSIX systems do not typically track creation time. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory.
-
Return Value:
- A dynamically allocated string representing the creation time of the directory in the format "YYYY-MM-DD HH:MM:SS".
NULL
: If an error occurs (e.g.,NULL
directory path, failure to retrieve the time, or unsupported platform).
-
Input Validation:
If thedirPath
isNULL
, the function logs an error and returnsNULL
. -
Error Handling:
If the directory path is invalid, or memory allocation fails, the function logs appropriate error messages and returnsNULL
.
- Purpose:
This function retrieves the current user's home directory path as a string. It supports both Windows and POSIX systems. The home directory is typically where user-specific files and configurations are stored. - Parameters:
- None.
- Return Value:
- A dynamically allocated string representing the home directory path.
NULL
: If an error occurs (e.g., failure to retrieve the home directory path or memory allocation failure).
- Error Handling:
The function logs appropriate error messages if it fails to retrieve or convert the home directory path and returnsNULL
in such cases.
-
Purpose:
This function determines the type of a file or directory at the specified path and returns a value from theDirFileType
enum, which can indicate whether the file is a regular file, directory, symbolic link, or an unknown type. -
Parameters:
filePath
: A pointer to a string representing the path to the file or directory.
-
Return Value:
- Returns one of the following enum values:
DIR_FILE_TYPE_REGULAR
: If the file is a regular file.DIR_FILE_TYPE_DIRECTORY
: If the file is a directory.DIR_FILE_TYPE_SYMLINK
: If the file is a symbolic link.DIR_FILE_TYPE_UNKNOWN
: If the file type could not be determined or the file does not exist.
- Returns one of the following enum values:
-
Input Validation:
If thefilePath
isNULL
, the function logs an error and returnsDIR_FILE_TYPE_UNKNOWN
. -
Error Handling:
If the file path is invalid or the file type cannot be determined, the function logs an error and returnsDIR_FILE_TYPE_UNKNOWN
.
-
Purpose:
This function encrypts the contents of a file using the DES encryption algorithm in CBC mode, with a specified password and initialization vector (IV). The encrypted data is written back to the original file. -
Parameters:
filePath
: A pointer to a string representing the path to the file to be encrypted.password
: A pointer to a string representing the encryption password (must be 8 bytes or fewer).iv
: A pointer to an 8-byte initialization vector (IV) used for encryption.
-
Return Value:
true
: If the file was successfully encrypted.false
: If an error occurs during the encryption process (e.g., file not found, memory allocation failure, or encryption failure).
-
Input Validation:
- The function checks if
filePath
,password
, oriv
areNULL
. If any of these areNULL
, it logs an error and returnsfalse
.
- The function checks if
-
Error Handling:
- If the file cannot be opened for reading or writing, memory allocation fails, the password is too long, or encryption fails, the function logs appropriate error messages and returns
false
.
- If the file cannot be opened for reading or writing, memory allocation fails, the password is too long, or encryption fails, the function logs appropriate error messages and returns
-
Purpose:
This function decrypts the contents of a file using the DES encryption algorithm in CBC mode. It uses the provided password and initialization vector (IV) to decrypt the file, then writes the decrypted data back to the original file. -
Parameters:
filePath
: A pointer to a string representing the path of the file to be decrypted.password
: A pointer to a string representing the decryption password (must be 8 bytes or fewer).iv
: A pointer to an 8-byte initialization vector used for decryption.
-
Return Value:
true
: If the file was successfully decrypted.false
: If an error occurs during the decryption process (e.g., invalid input, file not found, or decryption failure).
-
Input Validation:
The function checks iffilePath
,password
, oriv
areNULL
. If any of these areNULL
, it logs an error and returnsfalse
. -
Error Handling:
If the file cannot be opened, memory allocation fails, or decryption fails, the function logs appropriate error messages and returnsfalse
.
-
Purpose:
This function retrieves the owner of the specified file. It fills theownerBuffer
with the owner's name and ensures that the string does not exceed the size of the buffer. -
Parameters:
filePath
: A pointer to a string representing the path of the file.ownerBuffer
: A pointer to a buffer where the owner's name will be stored.bufferSize
: The size of theownerBuffer
.
-
Return Value:
true
: If the file owner was successfully retrieved.false
: If an error occurs (e.g., the file path is invalid, or the retrieval fails).
-
Input Validation:
The function checks iffilePath
,ownerBuffer
, orbufferSize
areNULL
or ifbufferSize
is 0. If any of these conditions are true, it logs an error and returnsfalse
. -
Error Handling:
If the file path is invalid, memory allocation fails, or the file owner cannot be retrieved, the function logs appropriate error messages and returnsfalse
.
-
Purpose:
This function retrieves the owner of the specified directory and stores the owner's name in the provided buffer. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory.ownerBuffer
: A pointer to a buffer where the owner's name will be stored.bufferSize
: The size of theownerBuffer
.
-
Return Value:
true
: If the directory owner was successfully retrieved.false
: If an error occurs (e.g., invalid input or failure to retrieve the owner).
-
Input Validation:
The function checks ifdirPath
,ownerBuffer
, orbufferSize
areNULL
or ifbufferSize
is 0. If any of these conditions are true, it logs an error and returnsfalse
. -
Error Handling:
If an error occurs, such as failure to open the directory or retrieve the owner, the function logs appropriate error messages and returnsfalse
.
-
Purpose:
This function searches a directory for files matching a specified pattern. It invokes a user-defined callback function for each matched file. -
Parameters:
dirPath
: A pointer to a string representing the path of the directory to search.pattern
: A pointer to a string representing the pattern to match files against (e.g., "*.txt").callback
: A function pointer to a user-defined callback function that is invoked for each matched file.userData
: A pointer to user-defined data that will be passed to the callback function.
-
Return Value:
true
: If the search was successful and the callback was invoked for matching files.false
: If an error occurs (e.g., invalid input or failure to search the directory).
-
Input Validation:
The function checks ifdirPath
,pattern
, orcallback
areNULL
. If any of these areNULL
, it logs an error and returnsfalse
. -
Error Handling:
If an error occurs, such as failure to open the directory or search for files, the function logs appropriate error messages and returnsfalse
.
To compile a program using the Dir library, include all necessary source files in your compilation command This command compiles with GCC using the C17 standard and enables various optimizations and warnings for better performance and code quality.
gcc -std=c17 -O3 -march=native -flto -funroll-loops -Wall -Wextra -pedantic -s -o main ./main.c ./dir/dir.c
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dir_name_utf8 = "./کایزن";
bool result = dir_make_directory(dir_name_utf8);
if (result) {
fmt_printf("Directory %s created successfully.\n", "کایزن");
}
else {
fmt_printf("Failed to create directory '%s'.\n", dir_name_utf8);
}
return 0;
}
Result
Directory کایزن created successfully.
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
const char* dir_path = "./کایزن";
char* dirName = dir_dir_name(dir_path);
if (dirName) {
fmt_printf("Directory name: '%s'\n", dirName);
free(dirName);
}
else {
fmt_printf("Failed to get directory name.\n");
}
return 0;
}
Result
Directory name: 'کایزن'
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
char* currentPath = dir_current_path();
if (currentPath) {
fmt_printf("Current directory path: '%s'\n", currentPath);
free(currentPath);
}
else {
fmt_printf("Failed to get current directory path.\n");
}
return 0;
}
Result
Current directory path: 'C:\Users\asus\Hell\Desktop\stack'
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
const char* dir_path = "C:\\Users\\Science\\Desktop\\projects\\C\\c_std"; // Current directory
int fileCount = dir_count(dir_path);
if (fileCount >= 0) {
fmt_printf("Total number of directories and files in '%s': %d\n", dir_path, fileCount);
}
else {
fmt_printf("Failed to count files and directories in '%s'.\n", dir_path);
}
return 0;
}
Result
Failed to count files and directories in 'C:\Users\Science\Desktop\projects\C\c_std'.
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
const char* relativePath = "./کایزن/amin.txt"; // .,/ or any path
char* absolutePath = dir_absolute_file_path(relativePath);
if (absolutePath) {
fmt_printf("Absolute file path: '%s'\n", absolutePath);
free(absolutePath);
}
else {
fmt_printf("Failed to get absolute file path.\n");
}
return 0;
}
Result
Absolute file path: 'C:\Users\asus\OneDrive\Desktop\stack\کایزن\amin.txt'
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
const char* cd = "date";
if (dir_cd(cd)) {
fmt_printf("Changed directory to 'date'\n");
char* currentPath = dir_current_path();
if (currentPath) {
fmt_printf("Current directory path: '%s'\n", currentPath);
free(currentPath);
}
else {
fmt_printf("Failed to get current directory path.\n");
}
}
else {
fmt_printf("Failed to change directory.\n");
}
if (dir_cd_up()) {
fmt_printf("Moved up one directory.\n");
char* currentPath = dir_current_path();
if (currentPath) {
fmt_printf("Current directory path: '%s'\n", currentPath);
free(currentPath);
}
else {
fmt_printf("Failed to get current directory path.\n");
}
}
else {
fmt_printf("Failed to move up directory.\n");
}
return 0;
}
Result
Failed to change directory.
Moved up one directory.
Current directory path: 'C:\Users\asus\OneDrive\Desktop'
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirName = "C:\\Users\\asus\\OneDrive\\Desktop\\stack\\کایزن";
if (dir_is_empty(dirName)) {
fmt_printf("Directory '%s' is empty.\n", dirName);
if (dir_remove_directory(dirName)) {
fmt_printf("Successfully removed directory '%s'.\n", dirName);
}
else {
fmt_printf("Failed to remove directory '%s'.\n", dirName);
}
}
else {
fmt_printf("Directory '%s' is not empty, cannot remove.\n", dirName);
}
return 0;
}
Result
Successfully removed directory 'C:\Users\asus\OneDrive\Desktop\stack\کایزن'.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirName = "C:\\Users\\asus\\OneDrive\\Desktop\\stack\\کایزن";
if (dir_remove_directory_recursive(dirName)) {
fmt_printf("Successfully removed directory '%s'.\n", dirName);
}
else {
fmt_printf("Failed to remove directory '%s'.\n", dirName);
}
return 0;
}
Result
Successfully removed directory 'C:\Users\asus\OneDrive\Desktop\stack\کایزن'.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* oldFileName = "./tester.txt";
const char* newFileName = "./amin.txt";
if (dir_rename(oldFileName, newFileName)) {
fmt_printf("File renamed successfully from '%s' to '%s'.\n", oldFileName, newFileName);
}
else {
fmt_printf("Failed to rename file '%s'.\n", oldFileName);
}
return 0;
}
Result
File renamed successfully from './tester.txt' to './amin.txt'.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* oldDirName = "./one";
const char* newDirName = "./کایزن";
if (dir_rename(oldDirName, newDirName)) {
fmt_printf("Directory renamed successfully from '%s' to '%s'.\n", oldDirName, newDirName);
}
else {
fmt_printf("Failed to rename directory '%s'.\n", oldDirName);
}
return 0;
}
Result
Directory renamed successfully from './one' to './کایزن'.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirPath = "./کایزن/amin.txt"; // Replace with the directory path you want to check
if (dir_is_file_exists(dirPath)) {
fmt_printf("File '%s' exists.\n", dirPath);
}
else {
fmt_printf("File '%s' does not exist.\n", dirPath);
}
return 0;
}
Result
File './کایزن/amin.txt'
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirPath = "./کایزن"; // Replace with the directory path you want to check
if (dir_is_directory_exists(dirPath)) {
fmt_printf("Directory '%s' exists.\n", dirPath);
}
else {
fmt_printf("Directory '%s' does not exist.\n", dirPath);
}
return 0;
}
Result
Directory './کایزن'
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* sourceDir = "./کایزن"; // Replace with your source directory path
const char* destinationDir = "C:\\Users\\asus\\OneDrive\\Desktop"; // Replace with your destination directory path
if (dir_make_directory("C:\\Users\\asus\\OneDrive\\Desktop\\کایزن")) {
if (dir_copy_directory(sourceDir, destinationDir)) {
fmt_printf("Directory copied successfully from '%s' to '%s'.\n", sourceDir, destinationDir);
}
else {
fmt_printf("Failed to copy directory from '%s' to '%s'.\n", sourceDir, destinationDir);
}
}
else {
fmt_printf("Can not create folder");
}
return 0;
}
Result
Directory copied successfully from './کایزن' to 'C:\Users\asus\OneDrive\Desktop'.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* sourceFile = "./کایزن/amin.txt"; // Replace with your source file path
const char* destinationFile = "C:\\Users\\Science\\Desktop\\amin.txt"; // Replace with your destination file path
if (dir_copy_file(sourceFile, destinationFile)) {
fmt_printf("File copied successfully from '%s' to '%s'.\n", sourceFile, destinationFile);
}
else {
fmt_printf("Failed to copy file from '%s' to '%s'.\n", sourceFile, destinationFile);
}
return 0;
}
Result
File copied successfully from './کایزن/amin.txt' to 'C:\Users\asus\OneDrive\Desktop\amin.txt'.
Example 15 : Calculate the total size of a directory, including all files and subdirectories dir_get_directory_size
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirPath = "./کایزن";
long long size = dir_get_directory_size(dirPath);
// Check for errors
if (size == -1) {
fmt_printf("Failed to calculate the size of directory '%s'.\n", dirPath);
}
else {
fmt_printf("Total size of directory '%s' is: %lld bytes.\n", dirPath, size);
}
return 0;
}
Result
Total size of directory './کایزن' is: 10 bytes. // data in amin.txt is Hello amin
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* filePath = "./کایزن/amin.txt";
long long size = dir_get_file_size(filePath);
// Check for errors
if (size == -1) {
fmt_printf("Failed to calculate the file size '%s'.\n", filePath);
}
else {
fmt_printf("Total size of file '%s' is: %lld bytes.\n", filePath, size);
}
return 0;
}
Result
Total size of file './کایزن/amin.txt' is: 10 bytes.
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main(){
const char *dirPath = "C:\\Users\\Science\\Desktop\\";
Vector *foundItems = vector_create(sizeof(char *));
dir_list_contents(dirPath, DIR_LIST_ALL, foundItems);
for (size_t i = 0; i < vector_size(foundItems); ++i){
char **item = (char **)vector_at(foundItems, i);
fmt_printf("%s\n", *item);
free(*item); // Remember to free the duplicated string
}
vector_deallocate(foundItems);
return 0;
}
Result
Dark Souls II Scholar of the First Sin.lnk
DARK SOULS REMASTERED.lnk
desktop.ini
Docker Desktop.lnk
ELDEN RING Shadow of the Erdtree.lnk
Lively Wallpaper.lnk
MayaVPN.lnk
Mirage.lnk
project
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirPath = "C:\\Users\\Science\\Desktop\\projects\\C\\c_std\\main.c";
// also you can set another path for checking dir
if (dir_is_file(dirPath)) {
fmt_printf("Yes \"%s\" is path", dirPath);
}
else {
fmt_printf("No it is\n");
}
return 0;
}
Result
Yes "C:\\Users\\Science\\Desktop\\projects\\C\\c_std\\main.c" is path
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* sourcePath = "C:\\Users\\Science\\Desktop\\projects\\C\\c_std\\کایزن";
const char* destPath = "C:\\Users\\Science\\Desktop\\one";
// also for moving files you can use "dir_move_file"
if (dir_move_directory(sourcePath, destPath)) {
fmt_printf("Move to new location");
}
else {
fmt_printf("not Move\n");
}
return 0;
}
Result
Move to new location
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* sourcePath = "C:\\Users\\Science\\Desktop\\new_one.txt";
fmt_printf("Last Modified Time is %s\n", dir_get_modified_time(sourcePath));
return 0;
}
Result
Last Modified Time is 2024-11-28 21:49:45
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* sourcePath = "C:\\Users\\Science\\Desktop\\new_one.txt";
fmt_printf("Creation Time is %s\n", dir_get_creation_time(sourcePath));
return 0;
}
Result
Creation Time is 2024-11-26 13:46:42
#include "dir/dir.h"
#include "fmt/fmt.h"
#include <stdlib.h>
int main() {
char* homeDirectory = dir_get_home_directory();
if (homeDirectory != NULL) {
fmt_printf("Home Directory: %s\n", homeDirectory);
free(homeDirectory);
}
else {
fmt_printf("Failed to get home directory.\n");
}
return 0;
}
Result
Home Directory: C:\Users\amin
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
DirFileType type = dir_get_file_type("./dir/dir.h");
switch (type) {
case DIR_FILE_TYPE_REGULAR:
fmt_printf("Regular File\n");
break;
case DIR_FILE_TYPE_DIRECTORY:
fmt_printf("Directory\n");
break;
case DIR_FILE_TYPE_SYMLINK:
fmt_printf("Symbolic Link\n");
break;
default:
fmt_printf("Unknown Type\n");
}
return 0;
}
Result
Regular File
#include "dir/dir.h"
#include "crypto/crypto.h"
#include "fmt/fmt.h"
int main() {
const char* filePath = "C:\\Users\\Science\\Desktop\\new_one.txt";
const char* password = "aminamin";
uint8_t iv[DES_BLOCK_SIZE]; // IV for CBC mode
crypto_generate_random_iv(iv, DES_BLOCK_SIZE);
if (dir_encrypt_file(filePath, password, iv)) {
fmt_printf("File encrypted successfully.\n");
}
else {
fmt_printf("Failed to encrypt the file.\n");
}
if (dir_decrypt_file(filePath, password, iv)) {
fmt_printf("File decrypted successfully.\n");
}
else {
fmt_printf("Failed to decrypt the file.\n");
}
return 0;
}
Result
File encrypted successfully
File decrypted successfully.
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* filePath = "C:\\Users\\Science\\Desktop\\new_one.txt";
char ownerBuffer[256]; // Buffer to store the owner's name
if (dir_get_file_owner(filePath, ownerBuffer, sizeof(ownerBuffer))) {
fmt_printf("The owner of '%s' is: %s\n", filePath, ownerBuffer);
}
else {
fmt_printf("Failed to retrieve the file owner for '%s'\n", filePath);
}
return 0;
}
Result
The owner of 'C:\\Users\\Science\\Desktop\\new_one.txt' is: asus
#include "dir/dir.h"
#include "fmt/fmt.h"
int main() {
const char* dirPath = "C:\\Users\\Science\\Desktop\\one";
char ownerBuffer[256]; // Buffer to store the owner's name
if (dir_get_directory_owner(dirPath, ownerBuffer, sizeof(ownerBuffer))) {
fmt_printf("The owner of '%s' is: %s\n", dirPath, ownerBuffer);
}
else {
fmt_printf("Failed to retrieve the file owner for '%s'\n", dirPath);
}
return 0;
}
Result
The owner of 'C:\\Users\\Science\\Desktop\\one' is: asus
#include "dir/dir.h"
#include "string/std_string.h"
#include "fmt/fmt.h"
#include <stdlib.h>
bool append_file_name_to_vector(const char* filePath, void* userData) {
Vector* vector = (Vector*)userData;
char* filePathCopy = string_strdup(filePath); // Duplicate the file path
if (!filePathCopy) {
fmt_fprintf(stderr, "Failed to duplicate file path");
return false;
}
vector_push_back(vector, &filePathCopy);
return true; // Continue the search
}
int main() {
const char* dirPath = "C:\\Users\\Science\\Desktop\\"; // Replace with your directory path
const char* pattern = "*.txt"; // Search pattern, e.g., all .txt files
Vector* foundFiles = vector_create(sizeof(char*));
if (!dir_search(dirPath, pattern, append_file_name_to_vector, foundFiles)) {
fmt_fprintf(stderr, "An error occurred during the search.\n");
}
else {
for (size_t i = 0; i < vector_size(foundFiles); ++i) {
char** filePath = (char**)vector_at(foundFiles, i);
fmt_printf("Found file: %s\n", *filePath);
free(*filePath); // Free the duplicated file path
}
}
vector_deallocate(foundFiles);
return 0;
}
Result
Found file: CMakeLists.txt
Found file: destination_file.txt
Found file: locked_file.txt
Found file: log.txt
Found file: new_log.txt
Found file: new_log_file.txt
Found file: old_log.txt
Found file: output.txt
Found file: test_uni.txt
Found file: text_uni.txt
#include "dir/dir.h"
#include "algorithm/algorithm.h"
#include "fmt/fmt.h"
void print_char(void *element) {
char** strPtr = (char**)element;
fmt_printf("%s\n", *strPtr);
}
int main(){
const char *dirPath = "C:\\Users\\Science\\Desktop\\";
Vector *foundItems = vector_create(sizeof(char *));
dir_list_contents(dirPath, DIR_LIST_ALL, foundItems);
algorithm_for_each(vector_begin(foundItems), vector_size(foundItems), sizeof(char*), print_char);
vector_deallocate(foundItems);
return 0;
}
Result
amin.txt
CapCut.lnk
Dark Souls II Scholar of the First Sin.lnk
DARK SOULS REMASTERED.lnk
desktop.ini
Docker Desktop.lnk
ELDEN RING Shadow of the Erdtree.lnk
Lively Wallpaper.lnk
Mirage.lnk
one
project
Project1
Silent Hill 2 Remake.lnk
This project is open-source and available under [ISC License].