Whitespaces to_underscores

White spaces in file and directory names had never been much of an issue for me until I began doing two new things: using gVim as my note-management tool, and putting up websites.  gVim finds files better if all the paths are without whitespaces, and media files in websites work smoother in general if their names are without whitespaces.  So, I wondered how to create a better workflow for intelligently replacing whitespaces with underscores in Windows 7.

Searching the web, I could find only one existing program that might do it, and a likely looking windows batch script.  Neither tempted me, and I got by with pasting a file’s basename into a line in gVim, and doing the substitution in that line with:

:s/ /_/g

– then pasting the name back onto the file.

To deal with more files at once, like image collections, I recently wrote my own Perl script, which recursively renames all files and directories in the directory that it’s run from.  It tidies up oddnesses like whitespace at the end of a basename, and what to do with whitespaces around hyphens.  Of course, being recursive, it’s dangerous (if run in a root directory for example), and so it includes a simulation option, and requires confirmation to actually go ahead.  I’m finding it particulary useful.

Share Button