Skip to content

Command line Options

Sam Matzko edited this page Dec 6, 2021 · 9 revisions

This page contains in-depth explanations of how the command-line arguments work, how to use them, and in some cases the source code relating to the argument.

The SOURCE and TARGET arguments

The SOURCE and TARGET arguments make up the base of all Sortery functionality, and are thus required. They are the two directories used in sorting. It depends on the arguments/flags you use, but generally, SOURCE is where the files-to-be-sorted are contained, and TARGET is the directory into which files are sorted.

The extract flag (-e, --extract)

The extract flag tells Sortery to move everything from SOURCE into TARGET, while maintaining subdirectory structure. For example: we have a directory with a structure that looks like this:

source/
source/
- target/
- - file4
- file1
- file2
- file3

If we want to move everything from source/ to target/, we would use the extract flag:

sortery source/ target/ -e

Sortery automatically detects if TARGET is inside SOURCE, and if it is, does not sort it. Now, source/ should look like this:

source/
source/
- target/
- - file4
- - file1
- - file2
- - file3

Here is the code for the extract flag, contained in src/tools.rs.

show
pub fn extract(source: &Path, target: &Path) {
    // Extract the contents of SOURCE to TARGET

    // The number of items we have moved
    let mut items_moved = 0;

    // Count the number of items we are going to move
    let mut items_to_move = 0;
    for entry in source.read_dir().expect("Failed to read dir") {

        // The entry path
        let entry = entry.expect("Failed to get dir entry.");
        let old_path = entry.path();

        // Make sure that the path being moved is not the source or target
        if old_path == source || old_path == target { continue }

        items_to_move += 1;
    }

    // The progress bar
    let progress_bar = ProgressBar {
        completed_message: String::from("Completed."),
        message: String::from("Extracting..."),
        total: items_to_move,
    };

    // Move each entry (file or directory) in the directory
    for entry in source.read_dir().expect("Failed to read dir.") {

        // The entry path
        let entry = entry.expect("Failed to get dir entry.");
        let old_path = entry.path();

        // Calculate the new path for the entry
        let new_path = target.join(old_path.file_name().unwrap());

        // Make sure that the path being moved is not the source or target
        if old_path == source || old_path == target { continue }

        // Move the path
        // println!("Moving {} to {}...", &old_path.display(), &new_path.display());
        fs::rename(old_path.display().to_string(), new_path.display().to_string())
            .expect(
                &error_messages::PathMoveFailedError {
                    source: &old_path,
                    target: &new_path,
                }.to_string()
            );
        
        // Add to the count of items moved
        items_moved += 1;

        // Show the progress
        progress_bar.set_progress(items_moved);
    }
    // Show success status
    progress_bar.complete();
    println!("Successfully moved {} items to {}.", items_moved, target.display());
}

Clone this wiki locally