DEV Community

Sandilya Bhamidipati
Sandilya Bhamidipati

Posted on

Sanitizing filenames for code

A lot of us must have seen this coming.SPACES IN FILENAMES. There is no escaping coz everyone does not follow sanity. However, part of an engineer/dev in you says let me fix this. There are a ton of ways on how to do that. As much as I prefer writing code to fix my problems, not everything needs code as I am firm believer in do not re-invent the wheel. Hence the following post.

The most generic pain-in-the-neck problems arise from space in filenames. I have used all possible solutions from using tr command to using automator in MacOS. However, not all solutions are applicable across all platforms. Hence, I ended using the detox command line utility. Here is why.

[~/Downloads/tmp/temp] $ tree .
.
├── file\ name\ 1.txt
├── file\ name\ 2.txt
├── file\ name\ 3.txt
└── file\ name\ 4.txt

[~/Downloads/tmp/temp] $ detox *.txt
[~/Downloads/tmp/temp] $ tree .
.
├── file_name_1.txt
├── file_name_2.txt
├── file_name_3.txt
└── file_name_4.txt
Enter fullscreen mode Exit fullscreen mode

I hope this elegant solution improves your productivity. Happy coding.

Top comments (1)

Collapse
 
elmuerte profile image
Michiel Hendriks • Edited

Why not "sanitizing code for filenames"?

Rule number ☃: do not trust user input.