Expand description
Convenient file and directory operations built on top of std::fs with improved error handling.
Includes copying or moving files and directories with progress reporting.
§Main features
- copy and move files or directories with:
- in-depth configuration options (existing destination file behaviour, IO buffering settings, copying depth, etc.), and
- progress reporting, if needed,
- scan directories (with options such as scan depth), and
- calculate file or directory sizes.
Visit the directory and file modules
for more information and a list of available functions.
§Feature flags
The following feature flags enable optional functionality:
dunce(enabled by default): enables the optionalduncesupport: This automatically strips Windows’ UNC paths if they can be represented using the usual type of path (e.g.\\?\C:\foo -> C:\foo) both internally and in e.g.DirectoryScan’s file and directory paths (this is recommended because path canonicalization very commonly returns UNC paths). This crate only has an effect when compiling for Windows targets.fs-err(disabled by default): enables the optionalfs-errsupport. Whilefs-morealready provides quite extensive error types, this does enable more helpful error messages for underlying IO errors.
§Examples
Copying a file and getting updates on the progress:
let source_path = Path::new("./source-file.txt");
let destination_path = Path::new("./target-file.txt");
let finished_copy = fs_more::file::copy_file_with_progress(
source_path,
destination_path,
FileCopyWithProgressOptions {
existing_destination_file_behaviour: ExistingFileBehaviour::Abort,
..Default::default()
},
|progress| {
let percent_copied =
(progress.bytes_finished as f64) / (progress.bytes_total as f64)
* 100.0;
println!("Copied {:.2}% of the file!", percent_copied);
}
)?;
match finished_copy {
FileCopyFinished::Created { bytes_copied } => {
println!("Copied {bytes_copied} bytes!");
}
FileCopyFinished::Overwritten { bytes_copied } => {
println!("Copied {bytes_copied} bytes over an existing file!");
}
// ...
_ => {}
};
Moving a directory and getting updates on the progress:
let source_path = Path::new("./source-directory");
let destination_path = Path::new("./target-directory");
let moved = fs_more::directory::move_directory_with_progress(
source_path,
destination_path,
DirectoryMoveWithProgressOptions {
destination_directory_rule: DestinationDirectoryRule::AllowEmpty,
..Default::default()
},
|progress| {
let percent_moved =
(progress.bytes_finished as f64) / (progress.bytes_total as f64)
* 100.0;
println!(
"Moved {:.2}% of the directory ({} files and {} directories so far).",
percent_moved,
progress.files_moved,
progress.directories_created
);
}
)?;
println!(
"Moved {} bytes ({} files, {} directories)! Underlying strategy: {:?}.",
moved.total_bytes_moved,
moved.files_moved,
moved.directories_moved,
moved.strategy_used
);Modules§
- Directory scanning, sizing, copying and moving operations. Includes progress monitoring variants.
- Error types provided by this library.
- File sizing, copying, moving and removal operations. Includes progress monitoring variants.
Macros§
- Imports (
uses)fs. This is only for internal use!