473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting files based on the last modified time

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
3 6486
numberwhun
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
also replied to on devshed.
Feb 28 '08 #3
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

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

Similar topics

8
by: Claudio Grondi | last post by:
It is maybe not a pure Python question, but I think it is the right newsgroup to ask for help, anyway. After connecting a drive to the system (via USB or IDE) I would like to be able to see...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
3
by: richardkreidl | last post by:
I have the following module that I delete old files based on how old they are: Sub Main() Dim First_Date As String = Date.Today.AddDays(-7) Dim Archive_Files() As String =...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
4
by: Chris Nethery | last post by:
Hello everyone, I have a challenging issue I need to overcome and was hoping I might gain some insights from this group. I am trying to speed up the process I am using, which is as follows: ...
7
by: elgiei | last post by:
Good morning at all, i have to implement a server,that every n-seconds (eg. 10sec) sends to other clients,which files and directory has been deleted or modified. i build a n-tree, for each...
2
by: Smithers | last post by:
I would appreciate some recommendations for programmatically determining if files differ. I'm writing a utility that backs up files that customers upload to Web sites. Rather than mindlessly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.