Home

Awesome

fs_extra

A Rust library that provides additional functionality not present in std::fs.

Build Status Crates.io Status Docs

Documentation

Migrations to 1.x.x version

Key features:

Functions:

FunctionDescription
fs_extra::copy_itemsRecursively copies files and directories from one location to another
fs_extra::copy_items_with_progressRecursively copies files and directories from one location to another with information about progress
fs_extra::move_itemsRecursively moves files and directories from one location to another
fs_extra::move_items_with_progressRecursively moves files and directories from one location to another with information about progress
fs_extra::remove_itemsRemoves files or directories
fs_extra::file::copyCopies the contents of one file to another
fs_extra::file::copy_with_progressCopies the contents of one file to another with information about progress
fs_extra::file::move_fileMoves a file from one location to another
fs_extra::file::move_file_with_progressMoves a file from one location to another with information about progress
fs_extra::file::removeRemoves a file
fs_extra::file::read_to_stringReads file content into a String
fs_extra::file::write_allWrites String content to a file
fs_extra::dir::createCreates a new, empty directory at the given path
fs_extra::dir::create_allRecursively creates a directory and all of its parent components if they are missing
fs_extra::dir::copyRecursively copies the directory contents from one location to another
fs_extra::dir::copy_with_progressRecursively copies the directory contents from one location to another with information about progress
fs_extra::dir::move_dirMoves directory contents from one location to another
fs_extra::dir::move_dir_with_progressMoves directory contents from one location to another with information about progress
fs_extra::dir::removeRemoves directory
fs_extra::dir::get_sizeReturns the size of the file or directory
fs_extra::dir::get_dir_contentGets details such as the size and child items of a directory
fs_extra::dir::get_dir_content2Gets details such as the size and child items of a directory using specified settings
fs_extra::dir::get_details_entryGets attributes of a directory entry
fs_extra::dir::lsGets attributes of directory entries in a directory

Usage

Add this to your Cargo.toml:

[dependencies]
fs_extra = "1.3.0"

Examples

The 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.

use std::path::Path;
use std::{thread, time};
use std::sync::mpsc::{self, TryRecvError};

extern crate fs_extra;
use fs_extra::dir::*;
use fs_extra::error::*;

fn example_copy() -> Result<()> {

    let path_from = Path::new("./temp");
    let path_to = path_from.join("out");
    let test_folder = path_from.join("test_folder");
    let dir = test_folder.join("dir");
    let sub = dir.join("sub");
    let file1 = dir.join("file1.txt");
    let file2 = sub.join("file2.txt");

    create_all(&sub, true)?;
    create_all(&path_to, true)?;
    fs_extra::file::write_all(&file1, "content1")?;
    fs_extra::file::write_all(&file2, "content2")?;

    assert!(dir.exists());
    assert!(sub.exists());
    assert!(file1.exists());
    assert!(file2.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));
            fs_extra::dir::TransitProcessResult::ContinueOrAbort
        };
        copy_with_progress(&test_folder, &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();
}