Solved

arrange files by date and add into an array

Posted on 2002-03-18
6
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:anant99
Comment Utility
thanks...however i wrote my own script for it...

thanks anyway

anant
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now