Solved

Check for newest file in a dir

Posted on 2001-08-13
12
761 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
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 97
remove duplicates from the csv file 13 109
rename outfile before writing 2 72
Vb script to unzip a files and rename the files 5 91
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

777 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