#sanitizer #filename

bin+lib sanitize-filename

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

8 releases (breaking)

0.7.0-beta Oct 1, 2025
0.6.0 Nov 7, 2024
0.5.0 Jul 31, 2023
0.4.0 May 7, 2022
0.1.0 Oct 24, 2018

#53 in Parser implementations

Download history 59196/week @ 2025-11-10 61953/week @ 2025-11-17 55997/week @ 2025-11-24 65471/week @ 2025-12-01 58568/week @ 2025-12-08 56482/week @ 2025-12-15 41238/week @ 2025-12-22 39279/week @ 2025-12-29 66933/week @ 2026-01-05 84196/week @ 2026-01-12 85538/week @ 2026-01-19 90067/week @ 2026-01-26 97299/week @ 2026-02-02 94411/week @ 2026-02-09 97189/week @ 2026-02-16 89860/week @ 2026-02-23

389,094 downloads per month
Used in 518 crates (114 directly)

MIT license

14KB
405 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

No runtime deps