4 Apr 2003
I’ll probably need this again sometime, and someone else might find it useful, so here’s a Perl regex that will strip invalid characters from a Windows filename.
s/[^\w\&%'`\-\@{}~!#\(\)&_\^\+,\.=\[\]]//g
also from a lazy perl hacker — Great idea, but the expression is too limiting (and you forgot to include the space char). Here’s an update —
!/usr/bin/perl Stripping illegal chars from Windows filenames. illegal chars from: http://en.wikipedia.org/wiki/Filename Bill Wong.use strict;
illegal character sets for various Windows systems.my $illegalHPFS = q#/|\?*#; my $illegalNTFS = $illegalHPFS . ‘\x00-\x1f’; my $illegalWIN95 = $illegalNTFS . ‘+’ . ‘\]’ . ‘\[‘;
=============================================== begin test ===============================================my $newname = “foo/bar|blech+[]\doo? [whop]*di \x06and \x10:colon”; print “newname = $newname\n”;
=============================================== pick the conversion that suits your needs: ===============================================$newname =~ s/[$illegalHPFS]/_/g; print “converted HPFS title = $newname\n”;
$newname =~ s/[$illegalNTFS]/_/g; print “converted NTFS title = $newname\n”;
$newname =~ s/[$illegalWIN95]/_/g; print “converted WIN95 title= $newname\n”;
Adam Kalsey
Mobile: 916.600.2497
Email: adam AT kalsey.com
AIM or Skype: akalsey
©1999-2008 Adam Kalsey.
Content management by Movable Type.
Guy
July 28, 2004 6:39 PM
Thanks dude, I was looking for a filename regexp and finally found one (yes, i’m a lazy perl hacker)