?
Solved

arrange files by date and add into an array

Posted on 2002-03-18
6
Medium Priority
?
272 Views
Last Modified: 2013-12-25
i have a folder which has lots of files.... i want to arrange them by date (newest first) and put them in an array....

help
Anant
0
Comment
Question by:anant99
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Peewee
ID: 6876460
anant99,

my @contents = system('ls -1pt');

a system command can get you everthing you need.

directories are suffixed by '/';
links by '@'

and the rest are files..

readdir can also get u this, but i'm not sure how there ordered..

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
    closedir DIR;


regards
peewee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6876876
anant99,

here' a complete example you can run on the command line.  It will give you the files list from modification date in the current directory for a unix type filesystem:

If you run this in a browser make sure you make the relevant cgi changes..ie path to perl, line break to <BR> etc..

#### get directory contents
 
 
 
print "start\n";
 
my @contents = `ls -1pt`;
 
foreach (@contents)
        {
                if (/.*[\/]|[@]/ig)
                {
                        push (@nonfiles, $_);
                }
                else
                {
                        push (@files, $_);
                }
        }
 
foreach (@files)
{
print "file:$_";
 
}
print "end\n";


let me know if you need more help.

peewee
0
 
LVL 16

Accepted Solution

by:
maneshr earned 400 total points
ID: 6877629
anant99,

". i want to arrange them by date (newest first) and put them  in an array...."

Here is a solution that is more portable across OS's.
This script sorts the files on the "Last modify time" of the file.

NOTE: i have commented the code so that you will find it easier to understand and maintain the same.

Let me know how this script works for you.
==============an99.pl
#!/usr/local/bin/perl

$dir="/full/path/to/directory";

if (-d $dir){
  opendir(DIR,$dir) || die $!;

  ##  Directory listing wihtout . and ..
  @allfiles=grep !/^\.\.?$/, readdir DIR;

  foreach(@allfiles){ ## Process entry by entry
    $file=$dir."/".$_;

    if (-f $file){    ##  Yes its a plain file
      @file_info=stat $file;
      $file_info=$file_info[9]."|".$file; ##  Last modify time | file name
      push(@output,$file_info);
    }
  }
  close(DIR);

  @output=reverse sort bynum @output; ##  Latest file on the top
  foreach(@output){
    ($file_mod_time,$file_name)=split(/\|/,$_);
    print $file_name,"\n";
  }
}else{
  print $dir," is not a directory!\n";
}

sub bynum{ $a <=> $b;}
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:anant99
ID: 6879819
thanks...however i wrote my own script for it...

thanks anyway

anant
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6880360
anant99,

"..however i wrote my own script for it....."

Can you pl. post that script here?

This would be helpful to any future user referencing this question.
0
 
LVL 1

Author Comment

by:anant99
ID: 6881725
im using the script for commercial purposes... thus i would not like to post it here. if u want , i can send it over at your email address..

anant
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

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