How do you validate filenames?

I got bit in the butt.

So here is my sob story. I have some old perl code that moves mp3 files and renames them based on the mp3 tag info. Well, I noticed the other day that a few songs had silenty disapeared during the move.

The culprit was failure to validate the filenames. If an invalid character was in the destination filename, the file was moved into oblivion with no error. oooopps...

So I did a quick search for some module that would validate filenames and could find nothing but this module VMS::FileUtils::SafeName that does not quite apply :) ...

So of course I wrote some quick code to do the job.. But was wondering how everyone else is handling this situation. I was suprised to not find a relevant module on CPAN but maybee I am just search impaired this evening.

Would also appreciate code review, since I suck with regexes! :)

use strict; use warnings; my $INV_CHAR; print "Testing for valid win32 filenames...\nThe following characters +are invalid.\n"; print '\\/:*?"<>|',"\n\n"; while (<DATA>) { chomp; print "filename: [",$_,"] is"; print validate_it($_) ? " valid.\n" : " not valid!\n\t Filename contains \"$INV +_CHAR\"\n" . "\ttry: [" . sanitize_it($_,'_') . "]\n"; } sub validate_it { local $_; $_ = shift; return /\\|\/|:|\*|\?|"|<|>|\|/ ? do { $INV_CHAR = $&; undef; } : 1; } sub sanitize_it { $_[0] =~ s/\\|\/|:|\*|\?|"|<|>|\|/$_[1]/g; return $_[0]; } __DATA__ file1 file2$is this)ok this-ok_too.. this-good.mp4 bad: b:a:d?--\ not<good> *is*bad/ ?what?is:good this|ok|not
UPDATE

To clear up any confusion about the purpose of this post. I'm not looking for solutions to the file copy/moving. I am looking for some discusion on handling filename validation/sanitizing.

My Questions are: (1) Do fellow perlmonks routinely validate filenames? If so, how do you do it? (2) Any inherit problems with how my routines deal with validating/sanitizing windows filenames??

zzSPECTREz


In reply to Validate windows filenames. by zzspectrez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.