1 stable release

1.0.1 Oct 14, 2024

#541 in Filesystem

MIT license

14KB
243 lines

File Split To Directory

split files to folders

Install

cargo install file-split-to-directory

Usage

Command Line

file-split-to-directory .
file-split-to-directory "/mnt/d/download"
# each folder 4400 files (default is 4400)
file-split-to-directory "/mnt/d/download" --chunk 4400
file-split-to-directory "/mnt/d/download" -c 4400

Result

from

- Download
-- 1.png
-- 2.png
-- 3.png
-- ....
-- 4401.png
-- ....
-- 8801.png

to

- Download
|- 0
||- 1.png
||- 2.png
||- 3.png
||- ...
|- 1
||- 4401.png
||- ...
|- 2
||- 8801.png

Use as library

Basic

use anyhow::Result;
use file_split_to_directory::FileSplitToDirectoryBuilder;
use std::num::NonZeroUsize;

fn main() -> Result<()> {
    FileSplitToDirectoryBuilder::default()
        .with_path("/tmp/test".into())
        .with_chunk(NonZeroUsize::new(100).unwrap())
        .build()? // std::io::Error if path = None
        .execute()?;
    Ok(())
}

Custom directory name

use anyhow::Result;
use file_split_to_directory::FileSplitToDirectoryBuilder;

fn main() -> Result<()> {
    FileSplitToDirectoryBuilder::default()
        .with_path("/tmp/test".into())
        .with_directory_name(|i: usize| i.to_string())
        .build()?
        .execute()?;
    Ok(())
}

Custom file order

use anyhow::Result;
use file_split_to_directory::FileSplitToDirectoryBuilder;
use std::fs::DirEntry;

fn main() -> Result<()> {
    // default order
    FileSplitToDirectoryBuilder::default()
        .with_path("/tmp/test".into())
        .with_sort_cmp(|a: &DirEntry, b: &DirEntry| {
            natord::compare( // cargo install natord
                &a.file_name().to_string_lossy(),
                &b.file_name().to_string_lossy(),
            )
        })
        .build()?
        .execute()?;

    // order file by size
    FileSplitToDirectoryBuilder::default()
        .with_path("/tmp/test".into())
        .with_sort_cmp(|a: &DirEntry, b: &DirEntry| {
            b.metadata()
                .map(|m| m.len())
                .unwrap_or_default()
                .cmp(&a.metadata().map(|m| m.len()).unwrap_or_default())
        })
        .build()?
        .execute()?;

    Ok(())
}

Dependencies

~1.7–2.3MB
~43K SLoC