Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

NotSoLong.pl

by ichimunki (Priest)
on Aug 12, 2001 at 03:53 UTC ( #104192=sourcecode: print w/ replies, xml ) Need Help??

Category: utility scripts
Author/Contact Info x@ichimunki.com
Description:
#!/usr/bin/perl -w
##################################################
#
# NotSoLong.pl - by ichimunki
#
# hereby released into Public Domain
# use at your own risk! tested on Linux only.
#
# usage: 'notsolong.pl [directory|filename]
# if no argument will use current directory
#
# purpose: recurse through a directory tree
# shortening both filenames and directory names
# to be 31 characters or less, so that they are
# visible via AppleTalk to Macs running pre-OS X
# versions of Mac OS.
#
# will normally just clip the name, but does 
# check to make sure the new, shorter name isn't
# already in use. if so, appends numbers until
# it gets a unique name.
#
# does not handle .paths at all, as these are 
# normally invisible to Mac OS
#
##################################################
use strict;
use File::Copy;
use File::Basename;
use File::Find;

@ARGV = ('.') unless @ARGV;

finddepth sub 
{
  if( ( length > 31 ) && -d && !/^\./ )
  {
    print "D: $_\n";
    my $short_name = substr( $_, 0, 31 );
    print "d: $short_name\n";
    change_name( $_, $short_name );
  }
  if( ( length > 31 ) && !-d && !/^\./ )
  {
    print "F: $_\n";
    my ($ext) = /.+?(\..+?)$/;
    my $short_name = substr( $_, 0, 31 - length($ext) ) . $ext;
    print "f: $short_name\n";
    change_name( $_, $short_name, $ext );
  }
}, @ARGV;

exit;

sub change_name
{
  my $old = shift or
    die "No too long name: $!";
  my $new = shift or
    die "No short enough name: $!";
  my $ext = shift || undef;

  my $counter = 1;

  while( stat( $new ) )
  {
    $counter++;
    if( $ext ) #this is a file
    {
      $new = substr( $new, 0, 31 - length($counter) - length($ext) ) .
      $counter . 
      $ext;
    }
    else #this is a directory handle
    {
      $new = substr( $new, 0, 31 - length($counter) ) . $counter;
    }
  }
  print "X: $new\n";
  move( $old, $new );
}

Comment on NotSoLong.pl
Download Code
Re: NotSoLong.pl
by ncw (Friar) on Aug 12, 2001 at 22:14 UTC
    You could as an option create an alternative tree with hardlinks in to the original files. That way you get to keep the original files unmodified just in case you need them again but you have a new hierarchy for the AppleTalkers to look at.

    PS: I've been there too with AppleTalk 32 char file names too! Apple file names can have control characters in which really confuses other platforms!

      That's a great idea! At least I know why I didn't do it this way -- when I go to shorten these filenames, it's so I can use my CD burner which is not usable from Linux (pesky USB peripherals!). Once they are archived these files are history. :)
      This would be great if you had a pattern to change in the file as well. Therefore, if you were shortening filenames with links in them, you could shorten the names, and not deaden the links. For instance, in html files: change a link from 365105&*)%$$$.php?_&&%GHFKHV to 1st_link.html and get the script to then change the filename, to 1st_link.html Cool, is it possible?

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://104192]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-07-24 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls