[nSLUG] Removing spaces from file names - note, long lines

Vlado Keselj vlado at dnlp.ca
Wed Nov 12 22:06:38 AST 2014


On Wed, 12 Nov 2014, Stephen Gregory wrote:

> On Wed, Nov 12, 2014 at 8:24 PM, Vlado Keselj <vlado at dnlp.ca> wrote:
> >
> > #!/usr/bin/perl
> > # Author: Vlado Keselj 2009-14 last update 2014-04-25
> > # Change file names to safe names (e.g., space to _)
> >
> > for my $fnold (@ARGV) {
> >     my $fnnew = $fnold;
> >
> >     local $_ = $fnnew; s/ +- +/-/g;
> >     s/''+/--/g; s/'/-/g; s/[[(<{]/_-/g; s/[])>}]/-_/g;
> >     s/[,:;]\s*/--/g; s/&/and/g; s/ /_/g;
> >     s/__+/_/g; s/---+/--/g;
> >     s/\xE2\x80\x99/-/g; # Single right quote
> >     s/(=|[^\w.-])/"=".uc unpack("H2",$1)/ge;
> >     $fnnew=$_;
> 
> 
> <insert joke about email corrupted by line noise here>
> 
> That seems to handle most of the annoyances in filenames, but does it
> handle files that start with a [-$*] or similar special?

Yes.  The final substitution:
s/(=|[^\w.-])/"=".uc unpack("H2",$1)/ge;
will catch any remaining characters which are not letters, digits, 
underscore, dot, or minus, and turn them into =XX where XX are hexadecimal 
digits of the character.  Combined with other substitutions, the a file 
named "[-$*]" is renamed to "_--=24=2A-_"

BTW, I see that:
s/(=|[^\w.-])/"=".uc unpack("H2",$1)/ge;
can be shortened into:
s/([^\w.-])/"=".uc unpack("H2",$1)/ge;

oh well...

Regards,
Vlado

> 
> -- 
> sg
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
> 


More information about the nSLUG mailing list