Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Finding list of files older than 30 min

Hi,

I am new to perl.

I need to find the list of xml files in few folders.

Where XML files are older than 30 min

I want the list of files in one mail from each folder

And then I wanted to mail the file

Please help me writting this is perl script

Many Thanks
Avatar of Fero45
Fero45

Try this. You can adjust the script easily
#!/usr/bin/perl
use strict;
use warnings;
 
my ($f, $mt, $fileAge);
 
opendir DIR, "myDir/" or die $!;
 
while (my $f = readdir(DIR)){
	next if $f =~ m/^\./;
	
	$mt = (stat $f)[9];
	$fileAge = time() - $mt;    
    print "File $f is $fileAge seconds old\n";
 
}
closedir(DIR);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oleber
oleber
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tia_kamakshi

ASKER

Many Thanks. I will try and come back to you.

How can we give mulitiple directory paths.

As there are many different directory path, where I need to check this.

Please guide

Many Thanks again
the paths are suposed to be in @DIR_PATHS

now depends how you need to pass them,
statically
@DIR_PATHS = ("path_1", "path_2", "path_3")
by script parameters
@DIR_PATHS = @ARGV

see you tomorow
Many Thanks

I will come back to you...
Many Thanks,

Will this script needs to be added in the crontab for every 30 min.

Or this script is itself is like schedular after 30 min

I think It would be great Idea if we execute this perl file from the schedular after every 30 min using crontab

So can you please remove schedular from the perl script

I dont wish mail, if there is no files in the folder

I am sorry for the trouble

Thanks again
Many Thanks
the time part is

my ( $mtime ) = (stat $file_path)[9];
next if $mtime + 30 * 60 < time(); # old enouf

remove it and you  shall not have problems
Hi,

When I execute the code you have proposed.

It gives me error below

abcdmcc@tgfr-tstva2:/usr/local/applic/cmc/admin$ perl ListAgedFiles.pl
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors.
abcdmcc@tgfr-tstva2:/usr/local/applic/cmc/admin$

Please guide

I am pasting my code in code part

Thanks
#!perl
use strict;
use warnings;
 
use MIME::Lite;
 
my @DIR_PATHS = qw(/home/defg/in /home/defg/out /home/abcdmcc/RegistrationRequest /home/abcdmcc/MessageRequest /home/abcdmcc/BrochureRequest /home/abcdmcc/EmailRequest);
 
 
my %old_files;
foreach my $dir_path ( @DIR_PATHS ) {
    opendir my $DIR, $dir_path or die $!;
 
    while (my $file_path = readdir(DIR)){
        next if not -f $file_path;         # return if it isn't a file
        push(@{$old_files{$dir_path}}, $file_path);
    }
    closedir($DIR);
}
 
 
while ( my ($dir_path, $files) = each %old_files ) {
    my $msg = MIME::Lite->new(
        From => 'dinesh.bali@pb.com',
        To => 'dinesh.bali@pb.com',
        Subject => "old files in $dir_path",
        Data => join("\n", @$files)
    );
 
    # send the email
    if ($I_DONT_HAVE_SENDMAIL) {
       MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
    }
    $msg->send();
}

Open in new window

how do you wich to send the mail, by a sendmail in the local machine? remove the

if ($I_DONT_HAVE_SENDMAIL) {
    MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
}


you have some smtp server, substitute

if ($I_DONT_HAVE_SENDMAIL) {
    MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
}

by

MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
Thanks for your quick response
Now I get the error

perl ListAgedFiles.pl
Name "main::DIR" used only once: possible typo at ListAgedFiles.pl line 14.

Please guide

Thanks for your co-operation
Avatar of ozo
Name "main::DIR" used only once: possible typo at ListAgedFiles.pl line 14 (#1)
    (W once) Typographical errors often show up as unique variable names.
    If you had a good reason for having a unique name, then just mention it
    again somehow to suppress the message.  The our declaration is
    provided for this purpose.
   
    NOTE: This warning detects symbols that have been used only once so $c, @c,
    %c, *c, &c, sub c{}, c(), and c (the filehandle or format) are considered
    the same; if a program uses $c only once but also uses any of the others it
    will not trigger this warning.



you readdir(DIR), but you never open it.
the scalar $DIR is different from the bareword DIR
Thanks for expalaining.

Can you please help me fixing this in my above program

Thanks again
I'd just use DIR for everything, but if you prefer, you could also use $DIR for everything
Question of taste.

Always use $DIR.

DIR becomes +- a global variable, so usually is a bad idea.