#filename #sanitizer #remove #name #node #file #command-line

bin+lib sanitize-filename

A simple filename sanitizer, based on Node's sanitize-filename

7 releases (breaking)

new 0.6.0 Nov 7, 2024
0.5.0 Jul 31, 2023
0.4.0 May 7, 2022
0.3.0 Oct 18, 2020
0.1.0 Oct 24, 2018

#48 in Parser implementations

Download history 21966/week @ 2024-07-18 24029/week @ 2024-07-25 25415/week @ 2024-08-01 26686/week @ 2024-08-08 24907/week @ 2024-08-15 23874/week @ 2024-08-22 26529/week @ 2024-08-29 26930/week @ 2024-09-05 26020/week @ 2024-09-12 23205/week @ 2024-09-19 30867/week @ 2024-09-26 29474/week @ 2024-10-03 28988/week @ 2024-10-10 34664/week @ 2024-10-17 37406/week @ 2024-10-24 36560/week @ 2024-10-31

142,793 downloads per month
Used in 193 crates (68 directly)

MIT license

12KB
298 lines

sanitize-filename

A basic filename sanitizer, based on Node's sanitize-filename.

Use like:

extern crate sanitize_filename;

fn main() {
    println!("{}", sanitize_filename::sanitize("some-user-defined/../../../string"));
    // prints some-user-defined......string
}

You can also configure a few basic options:

let options = sanitize_filename::Options {
    truncate: true, // true by default, truncates to 255 bytes
    windows: true, // default value depends on the OS, removes reserved names like `con` from start of strings on Windows
    replacement: "" // str to replace sanitized chars/strings
};

let sanitized = sanitize_filename::sanitize_with_options("some-user-defined/../../../string", options);

Also provides a basic command line binary. Use like:

cargo install sanitize-filename
sanitize-filename my_filename.txt
Pass a file name to clean to the program (also reads STDIN)

FLAGS:
    -r, --replace <r>          Replacement characters
    --windows, --no-windows    Whether to handle filenames for Windows
    --truncate, --no-truncate  Whether to truncate file names to 255 bytes

Dependencies

~2.7–4MB
~67K SLoC