README.md
1# fs_extra
2
3A Rust library that provides additional functionality not present in [`std::fs`](https://doc.rust-lang.org/std/fs/index.html).
4
5[![Build Status](https://travis-ci.org/webdesus/fs_extra.svg)](https://travis-ci.org/webdesus/fs_extra)
6[![Crates.io Status](https://img.shields.io/crates/v/fs_extra.svg)](https://crates.io/crates/fs_extra)
7[![Docs](https://docs.rs/fs_extra/badge.svg)](https://docs.rs/fs_extra)
8
9[Documentation](https://docs.rs/fs_extra)
10
11[Migrations to 1.x.x version](https://github.com/webdesus/fs_extra/wiki/Migrations-to-1.x.x-version)
12
13
14## Key features:
15
16* Copy files (optionally with information about the progress).
17
18* Copy directories recursively (optionally with information about the progress).
19
20* Move files (optionally with information about the progress).
21
22* Move directories recursively (optionally with information about the progress).
23
24* A single method for create and write `String` content in file.
25
26* A single method for open and read `String` content from file.
27
28* Get folder size
29
30* Get collection of directory entries
31
32## Functions:
33
34| Function | Description |
35| ------------- | ------------- |
36| [fs_extra::copy_items](https://docs.rs/fs_extra/*/fs_extra/fn.copy_items.html) | Recursively copies files and directories from one location to another |
37| [fs_extra::copy_items_with_progress](https://docs.rs/fs_extra/*/fs_extra/fn.copy_items_with_progress.html) | Recursively copies files and directories from one location to another with information about progress |
38| [fs_extra::move_items](https://docs.rs/fs_extra/*/fs_extra/fn.move_items.html) | Recursively moves files and directories from one location to another |
39| [fs_extra::move_items_with_progress](https://docs.rs/fs_extra/*/fs_extra/fn.move_items_with_progress.html) | Recursively moves files and directories from one location to another with information about progress |
40| [fs_extra::remove_items](https://docs.rs/fs_extra/*/fs_extra/fn.remove_items.html) | Removes files or directories |
41| [fs_extra::file::copy](https://docs.rs/fs_extra/*/fs_extra/file/fn.copy.html) | Copies the contents of one file to another |
42| [fs_extra::file::copy_with_progress](https://docs.rs/fs_extra/*/fs_extra/file/fn.copy_with_progress.html) | Copies the contents of one file to another with information about progress |
43| [fs_extra::file::move_file](https://docs.rs/fs_extra/*/fs_extra/file/fn.move_file.html) | Moves a file from one location to another |
44| [fs_extra::file::move_file_with_progress](https://docs.rs/fs_extra/*/fs_extra/file/fn.move_file_with_progress.html) | Moves a file from one location to another with information about progress |
45| [fs_extra::file::remove](https://docs.rs/fs_extra/*/fs_extra/file/fn.remove.html) | Removes a file |
46| [fs_extra::file::read_to_string](https://docs.rs/fs_extra/*/fs_extra/file/fn.read_to_string.html) | Reads file content into a `String` |
47| [fs_extra::file::write_all](https://docs.rs/fs_extra/*/fs_extra/file/fn.write_all.html) | Writes `String` content to a file |
48| [fs_extra::dir::create](https://docs.rs/fs_extra/*/fs_extra/dir/fn.create.html) | Creates a new, empty directory at the given path |
49| [fs_extra::dir::create_all](https://docs.rs/fs_extra/*/fs_extra/dir/fn.create_all.html) | Recursively creates a directory and all of its parent components if they are missing |
50| [fs_extra::dir::copy](https://docs.rs/fs_extra/*/fs_extra/dir/fn.copy.html) | Recursively copies the directory contents from one location to another |
51| [fs_extra::dir::copy_with_progress](https://docs.rs/fs_extra/*/fs_extra/dir/fn.copy_with_progress.html) | Recursively copies the directory contents from one location to another with information about progress |
52| [fs_extra::dir::move_dir](https://docs.rs/fs_extra/*/fs_extra/dir/fn.move_dir.html) | Moves directory contents from one location to another |
53| [fs_extra::dir::move_dir_with_progress](https://docs.rs/fs_extra/*/fs_extra/dir/fn.move_dir_with_progress.html) | Moves directory contents from one location to another with information about progress |
54| [fs_extra::dir::remove](https://docs.rs/fs_extra/*/fs_extra/dir/fn.remove.html) | Removes directory |
55| [fs_extra::dir::get_size](https://docs.rs/fs_extra/*/fs_extra/dir/fn.get_size.html) | Returns the size of the file or directory |
56| [fs_extra::dir::get_dir_content](https://docs.rs/fs_extra/*/fs_extra/dir/fn.get_dir_content.html) | Gets details such as the size and child items of a directory |
57| [fs_extra::dir::get_dir_content2](https://docs.rs/fs_extra/*/fs_extra/dir/fn.get_dir_content2.html) | Gets details such as the size and child items of a directory using specified settings |
58| [fs_extra::dir::get_details_entry](https://docs.rs/fs_extra/*/fs_extra/dir/fn.get_details_entry.html) | Gets attributes of a directory entry |
59| [fs_extra::dir::ls](https://docs.rs/fs_extra/*/fs_extra/dir/fn.ls.html) | Gets attributes of directory entries in a directory |
60
61## Usage
62
63Add this to your `Cargo.toml`:
64```toml
65[dependencies]
66fs_extra = "1.2.0"
67```
68## Examples
69
70The following example shows how to copy a directory recursively and display progress. First a source directory `./temp/dir` containing file `test1.txt` and a subdirectory `sub` is createad with `sub` itself having a file `test2.txt`. `./temp/dir` and all contents are then copied out to `./out/dir`.
71
72```rust
73use std::path::Path;
74use std::{thread, time};
75use std::sync::mpsc::{self, TryRecvError};
76
77extern crate fs_extra;
78use fs_extra::dir::*;
79use fs_extra::error::*;
80
81fn example_copy() -> Result<()> {
82
83 let path_from = Path::new("./temp");
84 let path_to = path_from.join("out");
85 let test_folder = path_from.join("test_folder");
86 let dir = test_folder.join("dir");
87 let sub = dir.join("sub");
88 let file1 = dir.join("file1.txt");
89 let file2 = sub.join("file2.txt");
90
91 create_all(&sub, true)?;
92 create_all(&path_to, true)?;
93 fs_extra::file::write_all(&file1, "content1")?;
94 fs_extra::file::write_all(&file2, "content2")?;
95
96 assert!(dir.exists());
97 assert!(sub.exists());
98 assert!(file1.exists());
99 assert!(file2.exists());
100
101
102 let mut options = CopyOptions::new();
103 options.buffer_size = 1;
104 let (tx, rx) = mpsc::channel();
105 thread::spawn(move || {
106 let handler = |process_info: TransitProcess| {
107 tx.send(process_info).unwrap();
108 thread::sleep(time::Duration::from_millis(500));
109 fs_extra::dir::TransitProcessResult::ContinueOrAbort
110 };
111 copy_with_progress(&test_folder, &path_to, &options, handler).unwrap();
112 });
113
114 loop {
115 match rx.try_recv() {
116 Ok(process_info) => {
117 println!("{} of {} bytes",
118 process_info.copied_bytes,
119 process_info.total_bytes);
120 }
121 Err(TryRecvError::Disconnected) => {
122 println!("finished");
123 break;
124 }
125 Err(TryRecvError::Empty) => {}
126 }
127 }
128 Ok(())
129
130}
131fn main() {
132 example_copy();
133}
134```
135