Solved

arrange files by date and add into an array

Posted on 2002-03-18
6
252 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
  • 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 100 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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