Solved

arrange files by date and add into an array

Posted on 2002-03-18
6
259 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 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python variable _ manually assigned 9 95
PowerShell and cisco ios 3 45
what should i learn? javascript or typescript? 3 38
Error when Running powershell script 6 31
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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to dynamically set the form action using jQuery.
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…

733 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