file-split-to-directory

Crates.iofile-split-to-directory
lib.rsfile-split-to-directory
version1.0.1
sourcesrc
created_at2024-10-14 08:59:50.270037
updated_at2024-10-14 08:59:50.270037
descriptionsplit files to folders
homepagehttps://github.com/hoshiyuki-tamako/file-split-to-directory-rs
repositoryhttps://github.com/hoshiyuki-tamako/file-split-to-directory-rs
max_upload_size
id1408103
size27,598
星雪靈兒 (hoshiyuki-tamako)

documentation

README

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(())
}
Commit count: 12

cargo fmt