[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Droplet to convert Mac filenames to WinNT format?



At 5.19 97/3/15, Darrell Berry wrote:
>Before I start to write one, is there a droplet with the following
>functionality:
>
>Drop a folder on it, and it will recursively check all filenames to see if
>they are WinNT-legal, and produce an error report of illegals, while also
>adding the appropriate MSOffice filetype suffix to each file based on its
>Mac file type & creator?
>
>---
>
>This set of requirements comes from moving a volume from a Mac fileserver
>to an NT machine, and introducing Windows machines...the filenaming
>conventions are different, and Win machines only identify filetypes by
>extensions...
>
>We are sharing Macs & PCs on the same network, but it makes more sense to
>run this utility on the Mac side, as the PCs will *not* open any of he
>files with nonlegal names...
>
>cheers
>
>----------------------------------------------------------------------------
>                                                >  d a r r e l l   b e r r y
>                                          http://shrine.cyber.ad.jp/~darrell

I wrote this for a friend of mine.  Feel free to edit it as needed (save it
as a droplet first!).  Handle with care, this code can be "dangerous;" it
would be best to run it on test directories first.  But my friend reports
great success in using it.

#!perl
$dir = shift;
%types = (      'TEXT','txt',
                        'FMP3','fp3',
                        'WDBN','doc',
                        'XLS4','xls',
                        'SLD3','ppt',
                        'ALB5','pm5',
                        'MPP ','mpp'
                );
&doStart($dir);

sub doStart {
        my($dir) = shift;
        opendir(D,$dir) || die "can't open $_: $!";
        foreach $file (readdir(D)) {
                if (-d "${dir}:${file}") {
                        &doStart("${dir}:${file}");
                } else {
                        my($yo) = '';
                        ($crea,$type) = MacPerl::GetFileInfo("${dir}:${file}");
                        foreach (keys %types) {
                                $yo = 1 if ($file =~ /\.$types{$_}$/);
                        }
                        if ($yo) {
                                ($nfile = $file) =~ s/(\..{3})$//;
                                $ext = $1;
                                $nfile =~ s/[\\\/:\*"<>\|\?\.]//g;
                                $nfile = "${nfile}${ext}";
                        } else {
                                $nfile = &doStuff($file,$dir,$type);
                        }
                        if (rename "${dir}:${file}","${dir}:${nfile}") {
#comment out this next line to disable printing of changed filenames
                                print "${dir}:${file} changed
to:\n${dir}:${nfile}\n\n" unless ($file eq $nfile);
                        } else {
#comment out this next line to disable printing of rename errors
                                print "Error renaming ${dir}:${file} to
${dir}:${nfile}: $!\n";
                        }
                }
        }
}
closedir(D);

sub doStuff {
        local($nfile,$dir,$type) = @_;
        $ext = $types{$type};
        $nfile = lc($nfile);
        $num = '';
        $nfile =~ s/[\\\/:\*"<>\|\?\.]//g;
        $tfile = "${nfile}${num}.${ext}";
        while ((-e "${dir}:$tfile") || (length($tfile) > 31)) {
                &doSame;
                &doLength;
                $tfile = "${nfile}${num}.${ext}";
        }
        return $tfile;
}

sub doSame {
        if (-e "${dir}:$tfile") {
                $num++;
        }
}

sub doLength {
        if (length($tfile) > 31) {
                chop($nfile);
        }
}
__END__


#================================================================
Chris Nandor                                      pudge@pobox.com
PGP Key 1024/B76E72AD                           http://pudge.net/
Keyfingerprint = 08 24 09 0B CE 73 CA 10  1F F7 7F 13 81 80 B6 B6