B
Bigus
Hi.
I have a script that recurses through a directory tree looking for OTF
fonts. When it finds one it extracts the postscript name and if the filename
is different then it attempts to rename the font file. However, it doesn't
rename the file and returns permission denied.
Strangely though, if I try the rename command stand-alone, ie: outside of
the loop where it runs through the files in the directory, it works. I'm
clutching at straws a bit here but is there some issue with renaming files
in a directory which is currently subject to the opendir command, or is it
something else entirely?
Here's the code:
----------------------------------------------
use strict;
use File::Copy;
use Font::TTF::Font;
recurser();
# -- recurse through 3 levels of folders -- #
sub recurser {
my $fromdir = "e:/software/fonts/";
my $fontext = "otf";
# level 1
opendir D1, $fromdir;
while( my $font1 = readdir(D1) ){
next if $font1 =~ /^\.+$/;
if(-d $fromdir.$font1){
# level 2
opendir D2, $fromdir.$font1;
while( my $font2 = readdir(D2) ){
next if $font2 =~ /^\.+$/;
# level 3
if(-d $fromdir.$font1.'/'.$font2){
opendir D3, $fromdir.$font1.'/'.$font2;
while( my $font3 = readdir(D3) ){
next if $font3 !~ /$fontext$/i;
renamefont
($fromdir.$font1.'/'.$font2.'/',$font3,$fontext);
}
closedir D3;
}
next if $font2 !~ /$fontext$/i;
renamefont ($fromdir.$font1.'/',$font2,$fontext);
}
closedir D2;
}
next if $font1 !~ /$fontext$/i;
renamefont ($fromdir,$font1,$fontext);
}
closedir D1;
}
# -- rename font -- ##
sub renamefont {
my($fontdir,$font,$ext) = @_;
my $f = Font::TTF::Font->open($fontdir.$font);
my $t = $f->{name}->read;
my $psname = $t->{strings}[6][1][0]{0};
if($psname =~ /\w+/ and $psname !~ /\s+/){
my $newfontname = $psname.'.'.$ext;
if($newfontname ne $font){
rename $fontdir.$font, $fontdir.$newfontname or
print 'rename '.$fontdir.$font.',
'.$fontdir.$newfontname.' failed<br>';
}
}
}
----------------------------------------------
As mentioned above if I just take the output from the failed rename command
in the above loop and run that on it's own, eg:
use strict;
rename "e:/software/fonts/M/MyriadPro-BlackSemiExt_____.otf",
"e:/software/fonts/M/MyriadPro-BlackSemiExt.otf";
it works.
Could someone tell me what's going wrong?
Thanks
Bigus
I have a script that recurses through a directory tree looking for OTF
fonts. When it finds one it extracts the postscript name and if the filename
is different then it attempts to rename the font file. However, it doesn't
rename the file and returns permission denied.
Strangely though, if I try the rename command stand-alone, ie: outside of
the loop where it runs through the files in the directory, it works. I'm
clutching at straws a bit here but is there some issue with renaming files
in a directory which is currently subject to the opendir command, or is it
something else entirely?
Here's the code:
----------------------------------------------
use strict;
use File::Copy;
use Font::TTF::Font;
recurser();
# -- recurse through 3 levels of folders -- #
sub recurser {
my $fromdir = "e:/software/fonts/";
my $fontext = "otf";
# level 1
opendir D1, $fromdir;
while( my $font1 = readdir(D1) ){
next if $font1 =~ /^\.+$/;
if(-d $fromdir.$font1){
# level 2
opendir D2, $fromdir.$font1;
while( my $font2 = readdir(D2) ){
next if $font2 =~ /^\.+$/;
# level 3
if(-d $fromdir.$font1.'/'.$font2){
opendir D3, $fromdir.$font1.'/'.$font2;
while( my $font3 = readdir(D3) ){
next if $font3 !~ /$fontext$/i;
renamefont
($fromdir.$font1.'/'.$font2.'/',$font3,$fontext);
}
closedir D3;
}
next if $font2 !~ /$fontext$/i;
renamefont ($fromdir.$font1.'/',$font2,$fontext);
}
closedir D2;
}
next if $font1 !~ /$fontext$/i;
renamefont ($fromdir,$font1,$fontext);
}
closedir D1;
}
# -- rename font -- ##
sub renamefont {
my($fontdir,$font,$ext) = @_;
my $f = Font::TTF::Font->open($fontdir.$font);
my $t = $f->{name}->read;
my $psname = $t->{strings}[6][1][0]{0};
if($psname =~ /\w+/ and $psname !~ /\s+/){
my $newfontname = $psname.'.'.$ext;
if($newfontname ne $font){
rename $fontdir.$font, $fontdir.$newfontname or
print 'rename '.$fontdir.$font.',
'.$fontdir.$newfontname.' failed<br>';
}
}
}
----------------------------------------------
As mentioned above if I just take the output from the failed rename command
in the above loop and run that on it's own, eg:
use strict;
rename "e:/software/fonts/M/MyriadPro-BlackSemiExt_____.otf",
"e:/software/fonts/M/MyriadPro-BlackSemiExt.otf";
it works.
Could someone tell me what's going wrong?
Thanks
Bigus