Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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;

    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");
      #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?
    [LanX]: sure
    [Corion]: 1nickt: Finding autobox in production would give me pause, yes
    [LanX]: efficient survey
    [MidLifeXis]: And under MINGW64_NT-6.1 MYHOST 2.6.0(0.304/5/3) 2016-09-09 09:46 x86_64 Msys there seem to be issues with escapes in external build tool calls.
    [Corion]: I mean, it's a technical feat it achieves, but... why? ;)
    [MidLifeXis]: And it also has the 0.14 version of the tarball in its manifest.
    [LanX]: avoiding unreadable brackets
    [MidLifeXis]: Although the previous one could be a b0rken PATH, I would need to dig for that.
    [thezip]: I've got to go to meetings now. If anyone has further comments regarding Spreadsheet::XLSX deployment to Strawberry Perl 5.24.1, please /msg me -- thanks!
    LanX has to go ... plans to crash with a car into a group of pythonistas while screaming "LARRY IS THE GREATEST"

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (13)
    As of 2017-03-23 17:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should Pluto Get Its Planethood Back?



      Results (291 votes). Check out past polls.