By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Sorting files based on the last modified time

P: 23
Hi All,

I have a remote directory wherein some files will be generated. I am writting a PERL script in windows to get the latest file created in that directory.How do i achieve this?

This is what i have tried so far...
Expand|Select|Wrap|Line Numbers
  1. use FindBin;
  2. use strict;
  3.  
  4. my $ScriptPath = $FindBin::Bin;  
  5. print "\n Script Path =  $ScriptPath ";
  6. my @files = "";
  7. ### remote directory
  8. my $dirPath = "D:\\Test\\TestMaintenance";
  9. opendir(DIRHANDLE, "$dirPath") or return("\n Can not open $dirPath \n $!");
  10. while ( defined (my $filename = readdir(DIRHANDLE)) ){
  11.     if($filename =~ m/TestMaintenance/){
  12.         print "\n$filename";
  13.         push(@files, $filename);
  14.     }
  15. }
  16. close DIRHANDLE;
  17.  
  18.  
looks like opendir gives random file list. Its not in a sorted order. How do i sort files in a directory based on the file modification time?

Thanks in advance.

Regards
Pramod
Feb 28 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi All,

I have a remote directory wherein some files will be generated. I am writting a PERL script in windows to get the latest file created in that directory.How do i achieve this?

This is what i have tried so far...
Expand|Select|Wrap|Line Numbers
  1. use FindBin;
  2. use strict;
  3.  
  4. my $ScriptPath = $FindBin::Bin;  
  5. print "\n Script Path =  $ScriptPath ";
  6. my @files = "";
  7. ### remote directory
  8. my $dirPath = "D:\\Test\\TestMaintenance";
  9. opendir(DIRHANDLE, "$dirPath") or return("\n Can not open $dirPath \n $!");
  10. while ( defined (my $filename = readdir(DIRHANDLE)) ){
  11.     if($filename =~ m/TestMaintenance/){
  12.         print "\n$filename";
  13.         push(@files, $filename);
  14.     }
  15. }
  16. close DIRHANDLE;
  17.  
  18.  
looks like opendir gives random file list. Its not in a sorted order. How do i sort files in a directory based on the file modification time?

Thanks in advance.

Regards
Pramod
Well, you can use Perl's 'sort()' function like this:

Expand|Select|Wrap|Line Numbers
  1. sort{ -M $b <=> -M $a }
  2.  
That will sort in reverse order by time. You could assign that to an array and your highest element is the newest and the first element is the oldest.

Regards,

Jeff
Feb 28 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
also replied to on devshed.
Feb 28 '08 #3

P: 23
Thanks Jeffin and Kevin.
I am working on this...
Facing some problems in accessing remote server, from where i have to read the files. i will look into this and get back to you.

You both always provide optimum solutions within short time:-)

Thanks again.

Pramod
Mar 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.