Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Syntactic Confectionery Delight
 
PerlMonks  

Re: Hide my ageing work

by periapt (Hermit)
on Oct 24, 2008 at 13:49 UTC ( #719340=note: print w/ replies, xml ) Need Help??


in reply to Hide my ageing work

oshalla and eight-one gave good descriptions of the grep block

The code almost works. Here are a couple of mods to make things work

  • The variable $file requires a my declaration
  • 'mv' is not a native perl function, use rename file1, file2. You could also use system() but, on my computer, that function seemed to work a little slower
  • You need to put double quotes around the second file otherwise perl interprets the '.' as the concatenation operator
  • Here is the modified code

    #! /usr/local/bin/perl use strict; use warnings; use diagnostics; die unless chdir "/path/directory"; die unless opendir DIR, '.'; foreach my $file (grep {-f && (14 < -M)} readdir DIR) { # print $file,"\n"; rename $file, ".$file"; # system("mv", $file, ".$file"); } closedir DIR;

    PJ
    use strict; use warnings; use diagnostics;


    Comment on Re: Hide my ageing work
    Download Code
    Re^2: Hide my ageing work
    by blowupp (Novice) on Oct 26, 2008 at 08:59 UTC
      With all the suggested weekend reading something has rubbed off & I have disturbed the Monks again to boast that I've cracked it!
      Here is my perl effort; A script to rename aged files, for the Monks archives;


      #! /usr/local/bin/perl
      use strict;
      use warnings;
      use File::Find;
      #set the age tolerance
      my $limit = 16;

      #call the find subroutine
      find (\&CheckFile, "/path to your directory");
      #subroutine
      sub CheckFile {
      $File::Find::name;
      my $age = -M;
      #test the age
      if (-f && ($age > $limit)) {
      print $File::Find::name;
      print " is ageing at ",int($age)," days old\n";
      rename ( $_, "XYZ.$_;") or die "rename failed: $!";
      }
      else {
      print $File::Find::name;
      print " is a youthful ",int($age)," days old\n";
      }
      }


    Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (9)
    As of 2014-04-17 10:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (444 votes), past polls