pub struct FileManager;Expand description
This is a file manager.
Implementations§
Source§impl FileManager
impl FileManager
Sourcepub fn create_file(path: &str) -> Result<File, Error>
pub fn create_file(path: &str) -> Result<File, Error>
Creates a new File.
§Arguments
path- The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
FileManager::create_file("file.txt").unwrap();
FileManager::create_file("C:\\user\\documents\\file.txt").unwrap();§Errors
If the file cannot be created, an error is returned.
Sourcepub fn write_to_file(path: &str, data: &str) -> Result<(), Error>
pub fn write_to_file(path: &str, data: &str) -> Result<(), Error>
Writes a string to a file.
§Arguments
path- The name of the file.(This will get the path of the root folder) or the absolute path of the file.content- The content to write to the file.
§Example
use filelib::FileManager;
FileManager::write_to_file("file.txt", "Hello World!").unwrap();
FileManager::write_to_file("C:\\user\\documents\\file.txt", "Hello World!").unwrap();§Errors
If the file cannot be written, an error is returned.
Sourcepub fn clean_file(path: &str) -> Result<(), Error>
pub fn clean_file(path: &str) -> Result<(), Error>
Cleans a file.
§Arguments
path- The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
FileManager::clean_file("file.txt").unwrap();
FileManager::clean_file("C:\\user\\documents\\file.txt").unwrap();§Errors
If the file cannot be cleaned, an error is returned.
Sourcepub fn read_file(path: &str) -> Result<String>
pub fn read_file(path: &str) -> Result<String>
Reads a file and returns its content.
§Arguments
path- The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
let cont1 = FileManager::read_file("file.txt").unwrap();
let cont2 = FileManager::read_file("C:\\user\\documents\\file.txt").unwrap();Sourcepub fn file_exists(path: &str) -> bool
pub fn file_exists(path: &str) -> bool
Sourcepub fn rename_file(path: &str, new_name: &str) -> Result<(), Error>
pub fn rename_file(path: &str, new_name: &str) -> Result<(), Error>
Renames a file
§Arguments
path- The path of the file.new_name- The new name of the file.
§Example
use filelib::{FileManager};
FileManager::rename_file("file.txt", "new_file.txt").unwrap();
FileManager::rename_file("C:\\user\\documents\\file.txt", "new_file.txt").unwrap();§Errors
If the file cannot be renamed, an error is returned.
Sourcepub fn change_extension(path: &str, new_extension: &str) -> Result<bool, Error>
pub fn change_extension(path: &str, new_extension: &str) -> Result<bool, Error>
Changes a file extension
§Arguments
path- The path of the file.new_extension- The new extension of the file.
§Example
use filelib::{FileManager};
FileManager::change_extension("file.txt", "rs").unwrap();
FileManager::change_extension("C:\\user\\documents\\file.txt", "rs").unwrap();§Errors
If the file cannot be renamed, an error is returned.
Sourcepub fn copy_file(path: &str, another_path: &str) -> Result<bool, Error>
pub fn copy_file(path: &str, another_path: &str) -> Result<bool, Error>
Copies a file
§Arguments
path- The path of the file.another_path- The path of the new file.
§Example
use filelib::{FileManager};
FileManager::copy_file("file.txt", "C:\\user\\documents\\file.txt").unwrap();
FileManager::copy_file("C:\\user\\documents\\file.txt", "C:\\user\\documents\\file.txt").unwrap();§Errors
If the file cannot be found, an error is returned.
If the paths have the same value , an error is returned.
Sourcepub fn create_dir(path: &str) -> Result<bool, Error>
pub fn create_dir(path: &str) -> Result<bool, Error>
Creates a directory
§Arguments
path- The path of the directory.
§Example
use filelib::{FileManager};
FileManager::create_dir("C:\\user\\documents\\some_dir").unwrap();
FileManager::create_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
FileManager::create_dir("some_dir3").unwrap();§Errors
If the directory cannot be created, an error is returned.
Sourcepub fn delete_dir(path: &str) -> Result<bool, Error>
pub fn delete_dir(path: &str) -> Result<bool, Error>
Deletes a directory
§Arguments
path- The path of the directory.
§Example
use filelib::{FileManager};
FileManager::delete_dir("C:\\user\\documents\\some_dir").unwrap();
FileManager::delete_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
FileManager::delete_dir("some_dir3").unwrap();§Errors
If the directory cannot be deleted, an error is returned.
Sourcepub fn rename_dir(path: &str, new_name: &str) -> Result<bool, Error>
pub fn rename_dir(path: &str, new_name: &str) -> Result<bool, Error>
Renames a directory
§Arguments
path- The path of the directory.new_name- The new name of the directory.
§Example
use filelib::{FileManager};
FileManager::rename_dir("C:\\user\\documents\\some_dir", "new_dir").unwrap();
FileManager::rename_dir("C:\\user\\documents\\some_dir\\some_dir2", "new_dir2").unwrap();
FileManager::rename_dir("some_dir3", "new_dir3").unwrap();§Errors
If the directory cannot be renamed, an error is returned.
Sourcepub fn copy_dir(path: &str, another_path: &str) -> Result<bool, Error>
pub fn copy_dir(path: &str, another_path: &str) -> Result<bool, Error>
Copies a directory
§Arguments
path- The path of the directory.another_path- The new path of the directory.
§Example
use filelib::{FileManager};
FileManager::copy_dir("C:\\user\\documents\\some_dir", "C:\\user\\documents\\some_dir2").unwrap();
FileManager::copy_dir("C:\\user\\documents\\some_dir\\some_dir2", "C:\\user\\documents\\some_dir2\\some_dir3").unwrap();
FileManager::copy_dir("some_dir3", "some_dir4").unwrap();§Errors
If the directory is not found, an error is returned.
If the paths have the same value , an error is returned.