Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!


Click here to return to the '10.4: Use PERL to keep the desktop clean' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
10.4: Use PERL to keep the desktop clean
Authored by: shemp9999 on Tue, Feb 7 2006 at 8:13PM PST
long ago i wrote an applescript to clean my desktop. once os x came around, i rewrote it in perl. it is easily adaptable for cleaning out other directories, too.

it simply checks for the file type using the unix 'file' command, and if that is not found, it uses the chars after the last dot in the name. then it copies the files into sorted folders in the destination dir. if a file already exists, it is skipped and is reported back to the shell. it could use more work to sort better, but i almost always find the file i want in a couple of clicks.

#!/usr/bin/perl -w

my $input = "/Users/shemp9999/Desktop"; # directory to clean
my $output = "/Users/shemp9999/Deskcrap"; # directory to move files to

my $file;
my $inf;
my @xtmp;
my $xtmp;
my $base;
my $ext;

my $item;

foreach $file (`ls \"$input\"`){
  chomp($file);
  
  # set ignore list for aliases, etc...
  
  $in = "$input\/$file"; #input path with filename
  $inf = `file \"$in\"`;
  chomp($inf);

  @item = split (/\:\s+/, $inf);

  $base = '';
  $ext = '';

  if ($file =~ /\./){ ## if there's a dot in the path/name
    @xtmp = split(/\./, $file);
    $ext = pop (@xtmp);
    foreach $xtmp (@xtmp){
      if ($base ne '') {$base .= '.' } ## restore dot
      $base .= $xtmp;
    }
  }else{  ## if there's no dot extension 
    $base = $file;
  }
 
  if ($ext ne ''){ 
    $dir = $ext;
  }else{
    $dir = $item[1];
  }
 
  $out = "$output\/$dir"; #input path with filename
  $outpath = "$out\/$file";

  unless (stat($outpath)){ ## if the file does not exist
    unless (stat($out)){ ## if the path does not exist
      `mkdir -p \"$out\"`;  ## make the path
    }
    `mv \"$in\" \"$outpath\"`;## move the file
  }else{ ## file exists
    print "file $outpath exists, skipping\n";
  } 
}


[ Reply to This | # ]
10.4: Use PERL to keep the desktop clean
Authored by: DMCrimson on Mon, May 8 2006 at 4:35AM PDT
Does not work... it moves all the files to destination directory, but does not sort them at all... Resukting error below

######ERROR########
Use of uninitialized value in string ne at sorter.pl line 40.Use of uninitialized value in concatenation (.) or string at sorter.pl line 46.
######ERROR########

[ Reply to This | # ]