A Rust library for more work functionality with file system.
Copy files with recept information about process.
Copy directory recursively and recept information about process.
Move files with recept information about process.
Move directories recursively and recept information about process.
One method for create and write String
content in file.
One method for open and read String
content from file.
fs_extra::fs::copy
fsextra::fs::copywith_progress
fsextra::fs::movefile
fsextra::fs::movefilewithprogress
fs_extra::fs::remove
fsextra::fs::readto_string
fsextra::fs::writeall
fs_extra::dir::create
fsextra::dir::createall
fs_extra::dir::copy
fsextra::dir::copywith_progress
fsextra::dir::movedir
fsextra::dir::movedirwithprogress
fs_extra::dir::remove
The following example shows how to copy a directory recursively and to follow the process.
This example created a directory dir
contains test1.txt
file and sub directory sub
. Folder sub
inside contains test2.txt
file.
Then copy ./temp/dir
and all containts to ./out/dir
```rust use std::path::{Path, PathBuf}; use std::{thread, time}; use std::sync::mpsc::{self, TryRecvError};
extern crate fsextra; use fsextra::dir::; use fs_extra::error::;
fn example_copy() -> Result<()> {
let mut path_from = PathBuf::from("./temp");
let test_name = "dir";
path_from.push("test_folder");
let mut path_to = path_from.clone();
path_to.push("out");
path_from.push(&test_name);
create_all(&path_from, true)?;
assert!(path_from.exists());
create_all(&path_to, true)?;
assert!(path_to.exists());
let mut file1_path = path_from.clone();
file1_path.push("test1.txt");
let content1 = "content";
fs_extra::file::write_all(&file1_path, &content1)?;
assert!(file1_path.exists());
let mut sub_dir_path = path_from.clone();
sub_dir_path.push("sub");
create(&sub_dir_path, true)?;
let mut file2_path = sub_dir_path.clone();
file2_path.push("test2.txt");
let content2 = "content2";
fs_extra::file::write_all(&file2_path, &content2)?;
assert!(file2_path.exists());
let mut options = CopyOptions::new();
options.buffer_size = 1;
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let handler = |process_info: TransitProcess| {
tx.send(process_info).unwrap();
thread::sleep(time::Duration::from_millis(500));
};
copy_with_progress(&path_from, &path_to, &options, handler).unwrap();
});
loop {
match rx.try_recv() {
Ok(process_info) => {
println!("{} of {} bytes",
process_info.copied_bytes,
process_info.total_bytes);
}
Err(TryRecvError::Disconnected) => {
println!("finished");
break;
}
Err(TryRecvError::Empty) => {}
}
}
Ok(())
} fn main() { example_copy(); }
```