[svlug] re-ordering entries in FAT directories

Tim tim at tetro.net
Fri Aug 30 18:35:33 PDT 2002


On Fri, Aug 30, 2002 at 03:54:22PM -0700, Marc MERLIN wrote:
> Is  there a  program  that can  recurse through  directories,  take all  the
> entries  and re-sort  them (probably  by  copying all  the files  to a  temp
> filename in the  order they need to  be in the directory,  and then renaming
> them)

I had written a little Perl script that recurses dirs, originally for
recursing a large directory tree made up of many small files and
creating "pathspec" lists of files totalling no more than 702 MB for use
with mkisofs's -path-list option.

Wasn't too much trouble to create what you want from it.  Usual
disclaimer applies.. "I can not be held liable for any damages..." yadda
yadda yadda.  I tested it on a small tree I mkdir'ed and touch'ed.  It
should work fine as long as the entire tree is on one file system.  Here
it is:


#!/usr/bin/perl -w

recurseAndSortDir(processArgs());
exit 0;


sub processArgs
{
  if($#ARGV != 0 && -d $ARGV[0])
  {
    print "Usage:\n"         .
          "       $0 <dir>\n";
    exit 1;
  }

  # get rid of trailing slash(es), if any, just so the path is purdy
  $ARGV[0] =~ s,/+$,,;

  if($ARGV[0] eq "." || $ARGV[0] eq "..") {
    print "Please don't use '.' or '..', thanks.\n";
    exit 1;
  }

  return $ARGV[0];
}


sub recurseAndSortDir
{
  local ( $curdir, $file );
  $curdir = $_[0];
  
  if( !opendir(local $dirhandle, "$curdir")                ||
      !rename($curdir, "$curdir-OLDDIR")                   ||
      !mkdir($curdir, (stat("$curdir-OLDDIR"))[2] & 07777) )  {
    print("$0: $curdir: $!\n");
    return;
  }

  print("*** Sorting $curdir/\n");

  foreach $file (sort readdir($dirhandle)) {
    if ( $file eq "." || $file eq ".." ) {
      next;
    }

    if(!rename("$curdir-OLDDIR/$file", "$curdir/$file")) {
      print("$0: rename(\"$curdir-OLDDIR/file\", ".
            "\"$curdir/$file\"): $!\n");
    }

    if ( -d "$curdir/$file" ) {
      recurseAndSortDir("$curdir/$file");
    }
  }

  if(!rmdir("$curdir-OLDDIR")) {
    print("$0: $curdir-OLDDIR: $!\n");
  }
  closedir($dirhandle);
}
# EOF


  - Tim



More information about the svlug mailing list