Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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;

    use strict; use warnings; use diagnostics;

    Replies are listed 'Best First'.
    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");
      sub CheckFile {
      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?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://719340]
    NodeReaper serves mincemeat tarts with the cider

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2018-04-24 13:27 GMT
    Find Nodes?
      Voting Booth?