469,353 Members | 2,116 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Age of the file

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
7 5773
KevinADC
4,059 Expert 2GB
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
rgolub
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
410 Expert 256MB
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
rgolub
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
971 Expert 512MB
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
971 Expert 512MB
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
4,059 Expert 2GB
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.

Similar topics

2 posts views Thread by matt | last post: by
7 posts views Thread by Joseph | last post: by
3 posts views Thread by StGo | last post: by
3 posts views Thread by Shapper | last post: by
reply views Thread by thjwong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.