Solved

Check for newest file in a dir

Posted on 2001-08-13
12
780 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 3

Expert Comment

by:bigbed
ID: 6380577
@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
ID: 6380989
Now I want to be sure that only files ending with .cart are checked! How to do that?
0
 

Author Comment

by:weversbv
ID: 6380997
The dir name will be $datadir. Where do I enter that??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:arogyarajxavier
ID: 6382467
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
ID: 6382488
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
ID: 6382715
print ((sort {$a->[1] <=> $b->[1] } map { [ $_, -M $_ ] } <$datadir/*.cart>)[0]->[0]);
0
 

Expert Comment

by:arogyarajxavier
ID: 6382801
ozo its simply great
0
 

Author Comment

by:weversbv
ID: 6383069
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
ID: 6383073
I used this line:

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

Expert Comment

by:arogyarajxavier
ID: 6383075
Give proper folder name to $datadir

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

Author Comment

by:weversbv
ID: 6383233
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
ID: 6384640
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

728 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