Professional Web Applications Themes

Testing for the existence of a file - PERL Beginners

Using Perl 5.6.1 on HP-UX. I need to test for the existence of a file every day. The filename changes each day because the filename contains a data and time stamp. For example, today's filename is KeyLinks_082903_120712.csv. So I would like to test for KeyLinks*.csv My code is as follows: ----- $file = "$DATA_HOME/KeyLinks\*.csv"; print $file; print "\n"; if (-e $file) { print "File Keylinks*.csv exists!\n"; } else { print "File Keylinks*.csv not understood!\n"; } For output, I get: --- /warehouse/data/KeyLinks*.csv File Keylinks.csv not understood! ---- It expands the filename and prints it out just fine, but the -e test ...

  1. #1

    Default Testing for the existence of a file

    Using Perl 5.6.1 on HP-UX.

    I need to test for the existence of a file every day. The filename
    changes each day because the filename contains a data and time stamp.
    For example, today's filename is KeyLinks_082903_120712.csv. So I would
    like to test for KeyLinks*.csv

    My code is as follows:

    -----

    $file = "$DATA_HOME/KeyLinks\*.csv";
    print $file;
    print "\n";

    if (-e $file) {
    print "File Keylinks*.csv exists!\n";
    } else {
    print "File Keylinks*.csv not understood!\n";
    }

    For output, I get:

    ---
    /warehouse/data/KeyLinks*.csv
    File Keylinks.csv not understood!
    ----

    It expands the filename and prints it out just fine, but the -e test
    fails. Why is that? I have reviewed several reference books and they all
    show examples of using the -e test. It mustn't be evaluating the splat
    correctly.

    Thanks,

    Kevin Struckhoff
    Data Warehouse Consultant
    Yamaha Motors U.S.
    [email]Kevin_Struckhoffyamaha-motor.com[/email]
    714-761-7310



    Kevin Struckhoff Guest

  2. #2

    Default RE: Testing for the existence of a file

    Sorry that this is ugly - I wrote it quickly one day...
    It checks for the file, and makes sure the date stamp is within 10 minutes.
    (Date::Manip was overkill for this). Who knows, I may have stolen this code
    from someone else -- nah...too ugly.

    #!/usr/bin/perl
    use File::stat;
    use Date::Manip;

    my $maxtime = 10; # in minutes, up to 59; for hours, HHMM... like '1200' for
    twelve hours, no minutes
    my $name = "/path/to/file/running.txt"; # status file name
    my $alert; # alert message holder/indicator if an alert is needed to be sent

    if (-e $name) { # does the file exist?
    my $filedate =localtime(stat($name)->mtime); # Get last modified date
    my $err;
    my $delta=&DateCalc($filedate,&PDate("now"),\$err ); # calc the time
    since modified from right now
    $delta =~ s/://g; # strip out separating colons
    $alert = "More than $maxtime minutes since CMS contacted webserver" if
    $delta > ($maxtime . '00');
    $alert = "Error: $err" if $err;

    } else {
    $alert = "$name doesn't exist";
    }

    my $status; # = 'OK';
    if ($alert) {
    $status = 'Error';
    warn localtime() . " - Status: $status\n";
    &sendEmailAlert($alert);
    }

    exit 0;

    ....then elsewhere I had sub sendEmailAlert defined.

    Also, I set crontab to run this every 10 minutes.

    -----Original Message-----
    From: Kevin Struckhoff [mailto:Kevin_Struckhoffyamaha-motor.com]
    Sent: Friday, August 29, 2003 10:24 AM
    To: [email]beginnersperl.org[/email]
    Subject: Testing for the existence of a file


    Using Perl 5.6.1 on HP-UX.

    I need to test for the existence of a file every day. The filename
    changes each day because the filename contains a data and time stamp.
    For example, today's filename is KeyLinks_082903_120712.csv. So I would
    like to test for KeyLinks*.csv

    My code is as follows:

    -----

    $file = "$DATA_HOME/KeyLinks\*.csv";
    print $file;
    print "\n";

    if (-e $file) {
    print "File Keylinks*.csv exists!\n";
    } else {
    print "File Keylinks*.csv not understood!\n";
    }

    For output, I get:

    ---
    /warehouse/data/KeyLinks*.csv
    File Keylinks.csv not understood!
    ----

    It expands the filename and prints it out just fine, but the -e test
    fails. Why is that? I have reviewed several reference books and they all
    show examples of using the -e test. It mustn't be evaluating the splat
    correctly.

    Thanks,

    Kevin Struckhoff
    Data Warehouse Consultant
    Yamaha Motors U.S.
    [email]Kevin_Struckhoffyamaha-motor.com[/email]
    714-761-7310



    perl@rjamestaylor.com Guest

  3. #3

    Default RE: Testing for the existence of a file

    Actually, using glob works easily enough:

    $file = glob "$DATA_HOME/KeyLinks\*.csv";
    print $file;
    print "\n";

    I now get as output:

    /warehouse/data/KeyLinks_082703_010101.csv

    Thanks.


    -----Original Message-----
    From: Robert J Taylor [mailto:robert.taylordis-sol-inc.com] On Behalf
    Of [email]perlrjamestaylor.com[/email]
    Sent: Friday, August 29, 2003 10:59 AM
    To: Kevin Struckhoff; [email]beginnersperl.org[/email]
    Subject: RE: Testing for the existence of a file


    Sorry that this is ugly - I wrote it quickly one day...
    It checks for the file, and makes sure the date stamp is within 10
    minutes. (Date::Manip was overkill for this). Who knows, I may have
    stolen this code from someone else -- nah...too ugly.

    #!/usr/bin/perl
    use File::stat;
    use Date::Manip;

    my $maxtime = 10; # in minutes, up to 59; for hours, HHMM... like '1200'
    for twelve hours, no minutes my $name = "/path/to/file/running.txt"; #
    status file name my $alert; # alert message holder/indicator if an alert
    is needed to be sent

    if (-e $name) { # does the file exist?
    my $filedate =localtime(stat($name)->mtime); # Get last modified
    date
    my $err;
    my $delta=&DateCalc($filedate,&PDate("now"),\$err ); # calc the
    time since modified from right now
    $delta =~ s/://g; # strip out separating colons
    $alert = "More than $maxtime minutes since CMS contacted webserver"
    if $delta > ($maxtime . '00');
    $alert = "Error: $err" if $err;

    } else {
    $alert = "$name doesn't exist";
    }

    my $status; # = 'OK';
    if ($alert) {
    $status = 'Error';
    warn localtime() . " - Status: $status\n";
    &sendEmailAlert($alert);
    }

    exit 0;

    ....then elsewhere I had sub sendEmailAlert defined.

    Also, I set crontab to run this every 10 minutes.

    -----Original Message-----
    From: Kevin Struckhoff [mailto:Kevin_Struckhoffyamaha-motor.com]
    Sent: Friday, August 29, 2003 10:24 AM
    To: [email]beginnersperl.org[/email]
    Subject: Testing for the existence of a file


    Using Perl 5.6.1 on HP-UX.

    I need to test for the existence of a file every day. The filename
    changes each day because the filename contains a data and time stamp.
    For example, today's filename is KeyLinks_082903_120712.csv. So I would
    like to test for KeyLinks*.csv

    My code is as follows:

    -----

    $file = "$DATA_HOME/KeyLinks\*.csv";
    print $file;
    print "\n";

    if (-e $file) {
    print "File Keylinks*.csv exists!\n";
    } else {
    print "File Keylinks*.csv not understood!\n";
    }

    For output, I get:

    ---
    /warehouse/data/KeyLinks*.csv
    File Keylinks.csv not understood!
    ----

    It expands the filename and prints it out just fine, but the -e test
    fails. Why is that? I have reviewed several reference books and they all
    show examples of using the -e test. It mustn't be evaluating the splat
    correctly.

    Thanks,

    Kevin Struckhoff
    Data Warehouse Consultant
    Yamaha Motors U.S.
    [email]Kevin_Struckhoffyamaha-motor.com[/email]
    714-761-7310



    Kevin Struckhoff Guest

Similar Threads

  1. SWF file different from internal movie testing
    By Laur56 in forum Macromedia Flash Player
    Replies: 0
    Last Post: May 17th, 09:33 PM
  2. Load Testing File Uploads
    By Scott in forum ASP Database
    Replies: 1
    Last Post: June 20th, 04:18 AM
  3. Test for file existence
    By Gregory Toomey in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 17th, 01:12 PM
  4. Best way to test for existence of a file
    By Bigdakine in forum UNIX Programming
    Replies: 4
    Last Post: July 13th, 05:15 PM
  5. CGI: how to test existence of file to upload?
    By J Krugman in forum PERL Miscellaneous
    Replies: 2
    Last Post: July 8th, 04:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139