?
Solved

Check for newest file in a dir

Posted on 2001-08-13
12
Medium Priority
?
781 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
Technology Partners: 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 400 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

Technology Partners: 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!

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 …
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…
Six Sigma Control Plans

752 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