Solved

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

Posted on 2012-04-12
11
453 Views
Last Modified: 2012-04-17
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
0
Comment
Question by:farooqhussain
  • 5
  • 3
  • 3
11 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37842885
Which particular part are you having problem with?
Existence, copying/moving, emailing?
0
 

Author Comment

by:farooqhussain
ID: 37842972
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37843188
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
 

Author Comment

by:farooqhussain
ID: 37845157
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 37845265
#!/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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Expert Comment

by:FishMonger
ID: 37845294
farzanj,

The 3 arg form of open works fine in 5.8.8 and should go back to at least 5.6.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37845343
Thanks FishMonger :)
0
 

Author Comment

by:farooqhussain
ID: 37845561
Thanks FishMonger... I will get back to you with the results once I test it..
0
 

Author Comment

by:farooqhussain
ID: 37851516
@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
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 37851621
#!/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
 

Author Closing Comment

by:farooqhussain
ID: 37858939
Thank you FishMonger for the script.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now