Solved

Check for newest file in a dir

Posted on 2001-08-13
12
738 Views
Last Modified: 2007-11-27
I have a dir with files in it.
I want to check the files and return the last modified file!

How to do that?
0
Comment
Question by:weversbv
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 3

Expert Comment

by:bigbed
Comment Utility
@files = sort {$a->[1] <=> $b->[1] }
         map { [ $_, -M $_ ] } <*>;
print $files[0]->[0];

# map generates a 2-dimensional list. Each entry contains
# filename and num of days since last modification
# (including partial days).
# Then we sort by age.
# So the first entry in the sorted list is the file most
# recently modified.
# So we print its name.
## does this help?
0
 

Author Comment

by:weversbv
Comment Utility
Now I want to be sure that only files ending with .cart are checked! How to do that?
0
 

Author Comment

by:weversbv
Comment Utility
The dir name will be $datadir. Where do I enter that??
0
 

Expert Comment

by:arogyarajxavier
Comment Utility
try this
opendir(DIR,$datadir);
@filenames=readdir(DIR);
closedir(DIR);

for(@filenames) {

     next if $_ eq '.';
     next if $_ eq '..';
     next if ($_ !~ /\.cart);

     push(@files,$_);
}

then use bigbed code.
0
 

Expert Comment

by:arogyarajxavier
Comment Utility
opendir(DIR,$datadir);
@filenames=readdir(DIR);
closedir(DIR);
chdir("$datadir");
for(@filenames) {

    next if $_ eq '.';
    next if $_ eq '..';
    next if ($_ !~ /\.cart/);

    push(@files,$_);
}

@files = sort {$a->[1] <=> $b->[1] }
        map { [ $_, -M $_ ] } <*>;
print $files[0]->[0];
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
print ((sort {$a->[1] <=> $b->[1] } map { [ $_, -M $_ ] } <$datadir/*.cart>)[0]->[0]);
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Expert Comment

by:arogyarajxavier
Comment Utility
ozo its simply great
0
 

Author Comment

by:weversbv
Comment Utility
Ozo:

I get the following error using your code:
Can't use an undefined value as an ARRAY reference

What is worng?
0
 

Author Comment

by:weversbv
Comment Utility
I used this line:

print ((sort {$a->[1] <=> $b->[1] } map { [ $_, -M $_ ] } <$datadir/*.cart>)[0]->[0]);
0
 

Expert Comment

by:arogyarajxavier
Comment Utility
Give proper folder name to $datadir

$datadir=?????????????????;
0
 

Author Comment

by:weversbv
Comment Utility
The error will occure when there is no .cart file in the dir! How to add an error control to this line?
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
if( @files = <$datadir/*.cart> ){
    print ((sort {$a->[1] <=> $b->[1] } map { [ $_, -M $_ ] } @files)[0]->[0]);
}else{
    print "no .cart file in the dir!";
}
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

10 Experts available now in Live!

Get 1:1 Help Now