Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check for newest file in a dir

Posted on 2001-08-13
12
Medium Priority
?
785 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

604 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