Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

arrange files by date and add into an array

Posted on 2002-03-18
6
Medium Priority
?
281 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 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
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

972 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