Loading ...
Sorry, an error occurred while loading the content.

X days old

Expand Messages
  • Dukelow, Don
    Is there a Perl module that has a command that will remove all files that are over X days old in a directory? Or will I have to write something? I ve try
    Message 1 of 6 , May 18 10:09 AM
    • 0 Attachment
      Is there a Perl module that has a command that will remove all files that are over X days old in a directory? Or will I have to write something?

      I've try using the UNIX "find" command from Perl but that is nasty.

      qx("/bin/find $data_dir -mtime $days -exec rm {} \;");


      -----
      Don Dukelow
      HP/GM GSC33 License Team
      Tel: 810-728-3388
      e-mail: dukelow@...
    • merlyn@stonehenge.com
      ... Dukelow, Is there a Perl module that has a command that will remove all files that are over X days old in a directory? Or will I have to write something?
      Message 2 of 6 , May 18 10:27 AM
      • 0 Attachment
        >>>>> "Dukelow," == Dukelow, Don <dukelow@...> writes:

        Dukelow,> Is there a Perl module that has a command that will remove all files that are over X days old in a directory? Or will I have to write something?

        Dukelow,> I've try using the UNIX "find" command from Perl but that is nasty.

        Dukelow,> qx("/bin/find $data_dir -mtime $days -exec rm {} \;");

        Grab my File::Finder from the CPAN, and you can turn that
        line almost directly into pure Perl:

        File::Finder->mtime("+$days")->exec(sub { unlink })->in($data_dir);


        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      • Wittich,Douglas G
        No guarantee, this is extracted from some of an application. I think it s complete, but you should verify and test all your code! my $LOGFILE_DIR =
        Message 3 of 6 , May 29 4:31 AM
        • 0 Attachment
          No guarantee, this is extracted from some of an application. I think it's complete, but you should verify and test all your code!

          my $LOGFILE_DIR = ...directory name where files are you want to delete...
          put_msg("D", "Purging log files from directory $LOGFILE_DIR.");
          if (opendir(LOGFILEDIR, $LOGFILE_DIR) == 0) {
          put_msg("E", "Can't open directory $LOGFILE_DIR.");
          return 1;
          }
          my @logfiles = readdir(LOGFILEDIR);
          closedir(LOGFILEDIR);
          my $fulllogfilepath;
          my $logfilect;
          foreach $logfile (@logfiles) {
          put_msg("D", "Considering $logfile");
          next if ($logfile eq '.' or $logfile eq '..');
          $logfilect++;
          $fulllogfilepath = "$LOGFILE_DIR/$logfile";
          if (-d $fulllogfilepath) {
          put_msg("E", "Skipping Directory. Expected a File not a Directory: $fulllogfilepath.");
          next;
          }
          if (-M $fulllogfilepath > 30) {
          put_msg("D", "File $logfile has older than 30 days and will be deleted.");
          unlink ($fulllogfilepath) || return 1;
          put_msg("I", "File $logfile has been deleted.");
          }
          }

          put_msg is a subroutine that does a print to a log file in this app.


          ________________________________
          From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com] On Behalf Of Dukelow, Don
          Sent: Monday, May 18, 2009 1:10 PM
          To: perl-beginner@yahoogroups.com
          Subject: [PBML] X days old




          Is there a Perl module that has a command that will remove all files that are over X days old in a directory? Or will I have to write something?

          I've try using the UNIX "find" command from Perl but that is nasty.

          qx("/bin/find $data_dir -mtime $days -exec rm {} \;");

          -----
          Don Dukelow
          HP/GM GSC33 License Team
          Tel: 810-728-3388
          e-mail: dukelow@...<mailto:dukelow%40hp.com>



          ==============================================================================
          This communication, including attachments, is confidential, may be subject to legal privileges, and is intended for the sole use of the addressee. Any use, duplication, disclosure or dissemination of this communication, other than by the addressee, is prohibited. If you have received this communication in error, please notify the sender immediately and delete or destroy this communication and all copies.


          [Non-text portions of this message have been removed]
        • Shlomi Fish
          ... You should use File::Find ( http://search.cpan.org/perldoc?File::Find ), File::Find::Object:
          Message 4 of 6 , May 29 6:14 AM
          • 0 Attachment
            On Friday 29 May 2009 14:31:29 Wittich,Douglas G wrote:
            > ________________________________
            > From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
            > On Behalf Of Dukelow, Don Sent: Monday, May 18, 2009 1:10 PM
            > To: perl-beginner@yahoogroups.com
            > Subject: [PBML] X days old
            >
            >
            >
            >
            > Is there a Perl module that has a command that will remove all files that
            > are over X days old in a directory? Or will I have to write something?
            >
            > I've try using the UNIX "find" command from Perl but that is nasty.
            >
            > qx("/bin/find $data_dir -mtime $days -exec rm {} \;");
            >

            You should use File::Find ( http://search.cpan.org/perldoc?File::Find ),
            File::Find::Object:
            http://www.shlomifish.org/open-source/projects/File-Find-Object/

            , File::Find::Rule (on the CPAN) or something. They are the Perl eqvuivalent
            to the UNIX find command. Let me know if you need more help.

            Are you interested in deleting from a directory (without the sub-directories)
            or throughout an entire directory tree?

            Regards,

            Shlomi Fish

            --
            -----------------------------------------------------------------
            Shlomi Fish http://www.shlomifish.org/
            Freecell Solver - http://fc-solve.berlios.de/

            God gave us two eyes and ten fingers so we will type five times as much as we
            read.
          • Robert Lee Binkley
            #!/usr/bin/perl # delold.pl - delete old files use strict; use warnings; use Win32::Autoglob; use Getopt::Long; $| = 1; sub print_usage; sub abort_usage; #
            Message 5 of 6 , May 29 3:08 PM
            • 0 Attachment
              #!/usr/bin/perl

              # delold.pl - delete old files

              use strict;
              use warnings;
              use Win32::Autoglob;
              use Getopt::Long;

              $| = 1;

              sub print_usage;
              sub abort_usage;

              # option defaults
              my $days_back = 10;
              my $test_flag = 0;
              my $verbose_flag = 0;
              my $quiet_flag = 0;

              # get options
              GetOptions (
              'days=i' => \$days_back,
              'test' => \$test_flag,
              'verbose' => \$verbose_flag,
              'quiet' => \$quiet_flag,
              'usage' => sub {print_usage; exit 1},
              ) or abort_usage "Invalid option";

              my $time = time();

              my $files_to_delete = 0;
              my $files_total = 0;
              my $files_failed = 0;

              FILE:
              for my $file (@ARGV) {

              # must exist, must be a plain old file
              next FILE if !-e $file;
              next FILE if !-f $file;

              $files_total++;

              # modified age in days
              my ($mtime) = (stat($file))[9];
              my $modified_age = ($time - $mtime) / (3600 * 24);

              # skip if too young
              next FILE if $modified_age < $days_back;

              $files_to_delete++;

              if ($test_flag || $verbose_flag) {
              printf "Age: %-6.1f File: %s\n",
              $modified_age,
              $file;
              }

              # skip if we are testing
              next FILE if $test_flag;

              # delete the file
              if (!unlink $file) {
              warn "Failed to delete file $file\n";
              $files_failed++;
              }

              }

              if ($test_flag) {
              print "\nTest Flag is set, no deletes done!\n";
              }

              my $files_remaining = $files_total - $files_to_delete + $files_failed;

              if (!$quiet_flag || $test_flag) {
              print "\n";
              print "Total files: $files_total\n";
              print "Files to delete: $files_to_delete\n";
              print "Failed to delete: $files_failed\n";
              print "Files remaining: $files_remaining\n";
              }

              exit 1;

              sub abort_usage {
              print STDERR join("\n", @_), "\n" if @_;
              print_usage;
              exit 0;
              }

              sub print_usage {
              print STDERR <<END;
              Usage: delold.pl [Options] files...
              Options:
              --days n - Set age of files to keep.
              Files over "days" old will be deleted.
              Default is 10 days.
              --test - Print file names to be deleted with age,
              but do not actually delete.
              Default is false.
              --verbose - Print file name and age while deleting.
              Default is false.
              --quiet - Suppress printing of totals after deleting.
              Default is false.
              --usage - print this message and exit
              END
              }


              -----Original Message-----
              From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
              On Behalf Of Shlomi Fish
              Sent: Friday, May 29, 2009 8:15 AM
              To: perl-beginner@yahoogroups.com
              Subject: Re: [PBML] RE: X days old

              On Friday 29 May 2009 14:31:29 Wittich,Douglas G wrote:
              > ________________________________
              > From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
              > On Behalf Of Dukelow, Don Sent: Monday, May 18, 2009 1:10 PM
              > To: perl-beginner@yahoogroups.com
              > Subject: [PBML] X days old
              >
              >
              >
              >
              > Is there a Perl module that has a command that will remove all files that
              > are over X days old in a directory? Or will I have to write something?
              >
              > I've try using the UNIX "find" command from Perl but that is nasty.
              >
              > qx("/bin/find $data_dir -mtime $days -exec rm {} \;");
              >

              You should use File::Find ( http://search.cpan.org/perldoc?File::Find ),
              File::Find::Object:
              http://www.shlomifish.org/open-source/projects/File-Find-Object/

              , File::Find::Rule (on the CPAN) or something. They are the Perl eqvuivalent

              to the UNIX find command. Let me know if you need more help.

              Are you interested in deleting from a directory (without the
              sub-directories)
              or throughout an entire directory tree?

              Regards,

              Shlomi Fish

              --
              -----------------------------------------------------------------
              Shlomi Fish http://www.shlomifish.org/
              Freecell Solver - http://fc-solve.berlios.de/

              God gave us two eyes and ten fingers so we will type five times as much as
              we
              read.



              ------------------------------------

              Unsubscribing info is here:
              http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links
            • merlyn@stonehenge.com
              ... Robert next FILE if !-e $file; Robert next FILE if !-f $file; If it s not -f, it s not -e. No point in checking both. -- Randal L. Schwartz -
              Message 6 of 6 , May 29 8:35 PM
              • 0 Attachment
                >>>>> "Robert" == Robert Lee Binkley <leebinkley@...> writes:

                Robert> next FILE if !-e $file;
                Robert> next FILE if !-f $file;

                If it's not -f, it's not -e. No point in checking both.

                --
                Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
              Your message has been successfully submitted and would be delivered to recipients shortly.