Re: Hide my ageing work
by gone2015 (Deacon) on Oct 23, 2008 at 19:39 UTC
|
-f "File is a plain file"
-M "Age of file (at startup) in days since modification
Testing $_ by default.
But what I really wanted to know was: how long have you been suffering from feelings of being hunted ?
| [reply] [d/l] [select] |
|
| [reply] |
Re: Hide my ageing work
by jettero (Monsignor) on Oct 23, 2008 at 19:33 UTC
|
It's actually the code-block fed to grep using file test operators (-f and -M). I'm a little surprised they work on $_ (implicitly) and actually think the second one should use _ (also discussed on the file test op page) instead of $_.
| [reply] |
Re: Hide my ageing work
by ccn (Vicar) on Oct 23, 2008 at 19:42 UTC
|
{-f && (14 < -M)} is equivalent of
{14 < -M $_ if -f $_;}
see: perldoc -X | [reply] [d/l] [select] |
Re: Hide my ageing work
by Your Mother (Archbishop) on Oct 23, 2008 at 20:58 UTC
|
You got some good answers already but you might try this-
use Acme::Botox;
But I keed!
I'm also with jettero in surprise that the code works right. readdir doesn't return paths, just names. From the pod-
If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have been testing the wrong
file.
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;
| [reply] [d/l] [select] |
|
Thanks for that, My script does not work and is only something I copied from the archives,
Probably should have made it clearer that I dont know what I'm doing and joined here for some pointers .... I'm still laughing at ccn's reply; surely most humorous :-)
Anyway, I will take the kind answers from all and try harder.
cheers
Mark
| [reply] |
Re: Hide my ageing work
by eighty-one (Curate) on Oct 23, 2008 at 19:40 UTC
|
| [reply] |
Re: Hide my ageing work
by periapt (Hermit) on Oct 24, 2008 at 13:49 UTC
|
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;
| [reply] [d/l] |
|
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";
}
}
| [reply] |