Age of the file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rgolub
    New Member
    • Mar 2008
    • 3

    Age of the file

    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.

    [CODE=perl]if( -f "$Dir/$FileName" ) {
    my($dev,$ino,$m ode,$nlink,$uid ,$gid,$rdev,$si ze,$atime,$mtim e,$ctime,$blksi ze,$blocks) = stat("$Dir/$FileName");
    my $time_diff = ($now - $ctime);[/CODE]

    Time_diff variables is always negative number/

    Please advise.
    Regards
    Last edited by eWish; Mar 3 '08, 11:30 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by rgolub
    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,$m ode,$nlink,$uid ,$gid,$rdev,$si ze,$atime,$mtim e,$ctime,$blksi ze,$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:

    Code:
    if( -f "$Dir/$FileName" ) {
          my($ctime) = (stat("$Dir/$FileName"))[10];
          print "ctime = $ctime , now = $now\n"
    }
    I assume $now is the value of time:

    Code:
    $now = time;

    Comment

    • rgolub
      New Member
      • Mar 2008
      • 3

      #3
      thank you for you respond,

      I don't get value of ctime, please see code below:
      -----------------------------------------------------------------------------------
      [CODE=perl]opendir(DIR,$di r_to_open) || die("Cannot open directory !\n");

      @dir_contents= readdir(DIR);
      closedir(DIR);

      foreach $file (@dir_contents)
      {
      if(!(($file eq ".") || ($file eq "..")))
      {
      my($ctime) = (stat("$file"))[10];
      print "ctime = $ctime\n";
      }
      }[/CODE]
      --------------
      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
      Last edited by eWish; Mar 4 '08, 02:41 PM. Reason: Please use code tags

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        The argument for stat() should contain absolute path to the file.
        Modify:

        Code:
        my($ctime) = (stat("$file"))[10];
        to

        Code:
        my($ctime) = (stat("$dir_to_open/$file"))[10];

        Comment

        • rgolub
          New Member
          • Mar 2008
          • 3

          #5
          I am getting a syntax error:

          [CODe=perl]#!/usr/bin/perl

          use Time::localtime ;
          use Time::Local;

          opendir(DIR,$di r_to_open) || die("Cannot open directory !\n");
          @dir_contents= readdir(DIR);
          closedir(DIR);

          foreach $file (@dir_contents)
          {
          if(!(($file eq ".") || ($file eq "..")))
          {
          my $now = time;
          my ctime = (stat($dir_to_o pen/$file)) [10];
          my $time_diff = ($now - $ctime);
          print "$file > Ctime: $ctime Diff: $time_diff\n";
          }
          }[/CODE]

          Code:
          No such class ctime at bs.pl line 33, near "my ctime"
          syntax error at bs.pl line 33, near "my ctime ="
          Execution of bs.pl aborted due to compilation errors.
          Last edited by eWish; Mar 4 '08, 04:54 PM. Reason: Please use code tags

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            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

            Comment

            • eWish
              Recognized Expert Contributor
              • Jul 2007
              • 973

              #7
              You forgot the '$' on the variable ctime.

              [CODE=perl] my $ctime = (stat($dir_to_o pen/$file)) [10];[/CODE]
              --Kevin

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by nithinpes
                The argument for stat() should contain absolute path to the file.
                Modify:

                Code:
                my($ctime) = (stat("$file"))[10];
                to

                Code:
                my($ctime) = (stat("$dir_to_open/$file"))[10];

                Thanks, I see I forget to mention he needs to insert the path and filename into the stat function.

                Comment

                Working...