Perl: Check if a file exists and email me if it does not.

Hi,

I am trying to write a Perl script which should do the following:

1. Check if a file "Test_Document_04122012.TXT" exists in a particular folder (H:\\Temp)
2. If the file exists, it should write it to a log that the "Test File exists"
3. If it does not exists, the script should email me saying "Test Document Not found"

I tried to write a perl script but it is not working as per my expectation. The code is attached.

I have ActivePerl 5.8.8 Build 822 installed on my Windows server 2003 SP2.

Please help !!!!
testfileexists.txt
farooqhussainAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
FishMongerConnect With a Mentor Commented:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);

my $path     = 'H:/temp';
my $filename = strftime("Test_Document_%m%d%Y.TXT", localtime);
my $logfile  = 'H:/File_exists.log';
my $admin    = '<emailid>';
my @files    = qw(Test_Document Repay_Document Manage_Document);

open my $log_fh, '>>', $logfile or die "failed to open '$logfile' <$!>";

foreach my $file ( @files ){
    file_check($file);
}

close $log_fh or die "failed to close $logfile <$!>";


sub file_check {
    my $filename = strftime("$_[0]_%m%d%Y.TXT", localtime);

    if (not -e "$path/$filename" ) {
        SendEmail($admin, 'Test File Error', "$filename not Found");
        warn "$path/$filename does not exist\n";
    
        print $log_fh "\n", '*' x 80, "\n";
        
        # other print statements as needed
    }
    
}

# more subroutine definitions below

Open in new window

0
 
farzanjCommented:
Which particular part are you having problem with?
Existence, copying/moving, emailing?
0
 
farooqhussainAuthor Commented:
I am having issue with Existence. I tested the script, and even when the "Test_Document_04122012.TXT" is present in the directory, it emails me with "Test Document not found". Can you please look at the code and tell me if there is any syntax error or any wrong logic in he script attached?

Thank You,
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
farzanjCommented:
Here is the part you are having trouble with.  I don't have Perl on Windows so I cannot test your script.

my $file_location = "H:\\Temp";
my $filename = "Test_Document_04122012.TXT";
my $logFile    = "h:\\File_exists.log";

if (-f "$file_location\$filename")
{
    open(my $ofh, ">>", $logfile) or die "Could not open the log file : $!";
    #Compose your message here
    my $log_msg = "File exists";
    print $ofh $log_msg;
}
else
{
    #Sending email here
}

Open in new window


Notes:
You could also use something like
my $message = GetDateString() . "File found\n";

If my open statement doesn't work with 5.8.8, try: open(my $ofh, ">>$logfile") ...
0
 
farooqhussainAuthor Commented:
Hi Farzanj,

I apologize for not specifying that the file is generated everyday and it is suffixed with the timestamp, for eg., today's file will be generated as "Test_Document_04132012.TXT", tomorrow's file as "Test_Document_04142012.TXT" and so on. There is a separate job that moves that file after sometime to a backup folder. So I need a script which should be generic and should only look for "Test_Document" in the filename. Once I have this script, I will then schedule this script to run everyday in Windows scheduler. I probably think we would need to include a wild character in the script. Do you have any ideas on how to tackle the file name with wild characters or how to tackle this requirement?

Thank You,
0
 
FishMongerCommented:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);

my $path     = 'H:/temp';
my $filename = strftime("Test_Document_%m%d%Y.TXT", localtime);
my $logfile  = "H:/File_exists.log";
my $admin    = '<emailid>';

if (not -e "$path/$filename" ) {
    SendEmail($admin, 'Test File Error', "Test Document not Found");
    die "$path/$filename does not exist\n";
}

open my $log_fh, '>>', $logfile or die "failed to open '$logfile' <$!>";

print $log_fh "\n", '*' x 80, "\n";

# other print statements as needed

close $log_fh or die "failed to close $logfile <$!>";

# subroutine definitions below

Open in new window

0
 
FishMongerCommented:
farzanj,

The 3 arg form of open works fine in 5.8.8 and should go back to at least 5.6.
0
 
farzanjCommented:
Thanks FishMonger :)
0
 
farooqhussainAuthor Commented:
Thanks FishMonger... I will get back to you with the results once I test it..
0
 
farooqhussainAuthor Commented:
@FishMonger,

The script worked perfectly....

Now can we incorporate to look for these three different files in the same folder (and email me if it did not exist) in this single script which you provided.

For eg,. below 3 files reside in the same H:\temp folder.

1. Test_Document_04162012.TXT
2. Repay_Document_04162012.TXT
3. Manage_Document_04162012.TXT

I Just do not want to have three different scripts/jobs to run to look for three files in the same folder.

Do you think it is possible?
0
 
farooqhussainAuthor Commented:
Thank you FishMonger for the script.
0
All Courses

From novice to tech pro — start learning today.