zzspectrez has asked for the wisdom of the Perl Monks concerning the following question:
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
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Validate windows filenames.
by duelafn (Parson) on Aug 01, 2005 at 17:50 UTC | |
by zzspectrez (Hermit) on Aug 04, 2005 at 04:26 UTC | |
Re: Validate windows filenames.
by Anonymous Monk on Jul 14, 2005 at 07:01 UTC | |
by zzspectrez (Hermit) on Jul 14, 2005 at 07:07 UTC | |
by jbrugger (Parson) on Jul 14, 2005 at 07:48 UTC | |
by zzspectrez (Hermit) on Jul 14, 2005 at 16:02 UTC |