By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Age of the file

P: 3
I am trying define age of the file in seconds. If file > 900 (15 minutes), I would like get an alert. OS is Windows. For some reason I am getting negative number when I compare System time with file time.

Expand|Select|Wrap|Line Numbers
  1. if( -f "$Dir/$FileName" ) {
  2.       my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$Dir/$FileName");
  3.        my $time_diff = ($now - $ctime);
Time_diff variables is always negative number/

Please advise.
Regards
Mar 3 '08 #1
Share this Question
Share on Google+
7 Replies


KevinADC
Expert 2.5K+
P: 4,059
I am trying define age of the file in seconds. If file > 900 (15 minutes), I would like get an alert. OS is Windows. For some reason I am getting negative number when I compare System time with file time.

if( -f "$Dir/$FileName" ) {
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$a time,$mtime,$ctime,$blksize,$blocks) = stat("$Dir/$FileName");
my $time_diff = ($now - $ctime);

Time_diff variables is always negative number/

Please advise.
Regards

for right now do this and what values are printed for $ctime and $now:

Expand|Select|Wrap|Line Numbers
  1. if( -f "$Dir/$FileName" ) {
  2.       my($ctime) = (stat("$Dir/$FileName"))[10];
  3.       print "ctime = $ctime , now = $now\n"
  4. }
  5.  
I assume $now is the value of time:

Expand|Select|Wrap|Line Numbers
  1. $now = time;
Mar 3 '08 #2

P: 3
thank you for you respond,

I don't get value of ctime, please see code below:
-----------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. opendir(DIR,$dir_to_open) || die("Cannot open directory !\n");
  2.  
  3. @dir_contents= readdir(DIR);
  4. closedir(DIR);
  5.  
  6. foreach $file (@dir_contents)
  7.  {
  8.    if(!(($file eq ".") || ($file eq "..")))
  9.    {
  10.          my($ctime) = (stat("$file"))[10];
  11.          print "ctime = $ctime\n";
  12.     }
  13.  }
--------------
Getting ctime:
ctime =
ctime =
ctime =
ctime =
ctime =
ctime =
I am not sure what I am doing wrong, I am not strong with perl.
Thank you again for your help
Regards
Mar 4 '08 #3

nithinpes
Expert 100+
P: 410
The argument for stat() should contain absolute path to the file.
Modify:

Expand|Select|Wrap|Line Numbers
  1. my($ctime) = (stat("$file"))[10];
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. my($ctime) = (stat("$dir_to_open/$file"))[10];
  2.  
Mar 4 '08 #4

P: 3
I am getting a syntax error:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Time::localtime;
  4. use Time::Local;
  5.  
  6. opendir(DIR,$dir_to_open) || die("Cannot open directory !\n");
  7. @dir_contents= readdir(DIR);
  8. closedir(DIR);
  9.  
  10. foreach $file (@dir_contents)
  11.  {
  12.    if(!(($file eq ".") || ($file eq "..")))
  13.    {
  14.      my $now = time;
  15.      my ctime = (stat($dir_to_open/$file)) [10];
  16.      my $time_diff = ($now - $ctime);
  17.       print "$file > Ctime: $ctime              Diff: $time_diff\n";
  18.     }
  19. }
Expand|Select|Wrap|Line Numbers
  1. No such class ctime at bs.pl line 33, near "my ctime"
  2. syntax error at bs.pl line 33, near "my ctime ="
  3. Execution of bs.pl aborted due to compilation errors.
Mar 4 '08 #5

eWish
Expert 100+
P: 971
rgolub,

When posting code here you are expected to use the [code][/code] tags. Please read the post at the top of the forum "Guidelines To Ensuring Better Answers and Assistance To Your Thread(s)"

--Kevin
Mar 4 '08 #6

eWish
Expert 100+
P: 971
You forgot the '$' on the variable ctime.

Expand|Select|Wrap|Line Numbers
  1.  my $ctime = (stat($dir_to_open/$file)) [10];
--Kevin
Mar 4 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
The argument for stat() should contain absolute path to the file.
Modify:

Expand|Select|Wrap|Line Numbers
  1. my($ctime) = (stat("$file"))[10];
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. my($ctime) = (stat("$dir_to_open/$file"))[10];
  2.  

Thanks, I see I forget to mention he needs to insert the path and filename into the stat function.
Mar 4 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.