Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:04:58+00:00 2026-06-10T03:04:58+00:00

I’m trying to assemble a script that will first identify the newest log file

  • 0

I’m trying to assemble a script that will first identify the newest log file created under a folder, then open it and look for specific data. Basically, I will be looking in this log file for a specific error and print the errors into the new log file.

I understand how to perform sort in order to have the most recent file, but having trouble in reading the latest file and copying it to the new log file

use File::stat;

$dirname  = 'C:/Luntbuild_Logs';
$timediff = 0;

opendir DIR, "$dirname";

while ( defined( $file = readdir(DIR) ) ) {
    if ( $file ne "." && $file ne ".." ) {
        $diff = time() - stat("$dirname/$file")->mtime;
        if ( $timediff == 0 ) {
            $timediff = $diff;
            $newest   = $file;
        }
        if ( $diff < $timediff ) {
            $timediff = $diff;
            $newest   = $file;
        }
    }
}

print $newest;

$file1 = "$dirname/$file";

open( FILE1, "<$newest" );
my (@fprint) = <FILE1>;
close FILE1;

open( FOUT, ">list1.txt" ) || die("Cannot Open File");

foreach $line (@fprint) {
    print "$line" if $line =~ /> @/;
    print "$line" if $line =~ /ORA-/;
    print FOUT $line;
}

close FOUT;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:05:00+00:00Added an answer on June 10, 2026 at 3:05 am

    The primary problem is that you build the full file path in $file1 but then open $newest which contains only the file name.

    You really should improve your code by

    • Always using use strict and use warnings at the start of your program, and declaring all variables with my at their point of definition

    • Use lexical file and directory handles and the three-parameter form of open. Always test the status of an open and put $! in the die string so that you know why it failed

    • Never put double-quotes around a variable unless you know what it does and that is what you want

    Here is a refactoring of your code which follows these guidelines and also uses the built-in -M operator that returns the age of a file in days

    use strict;
    use warnings;
    
    my $dirname = 'C:/Luntbuild_Logs';
    my ($newest_age, $newest_file);
    
    opendir my $dh, $dirname or die $!;
    
    while ( readdir($dh) ) {
      my $file = "$dirname/$_";
      next unless -f $file;
      my $age = -M $file;
      unless (defined $newest_age and $newest_age <= $age) {
        $newest_age = $age;
        $newest_file = $file;
      }
    }
    
    print $newest_file, "\n";
    
    open my $out,'>', 'list1.txt' or die "Cannot open file: $!";
    
    open my $in, '<', $newest_file or die $!;
    while (<$in>) {
      print if /> \@/ or /ORA-/;
      print { $out } $_;
    }
    
    close $out or die $!;
    close $in or die $!;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.