473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading a Windows dir into an array, sorting by date, grabbing most recent

2 New Member
Hello,
I'm trying to do a Perl script on Windows (wish it was Unix) that goes to a directory, figures out which files are most recent, within the last week, then copies them to another dir. I'm stuck at the sorting of the files in a directory. This part of the code returns a list but it's not sorted by date:

Expand|Select|Wrap|Line Numbers
  1. opendir(INPUTINFO, ".") or die "Doesn't work: $!";
  2.    my @xmlfiles = grep /\.xml$/, readdir INPUTINFO;
  3.    my@sorted = 
  4.     map {join'/',(split/-/)[1,2,0]}
  5.     sort
  6.     map {join'-',(split'/')[2,0,1]}
  7.         qw(12/31/2000 12/26/2000 01/01/2001), @xmlfiles;
  8.        print "$_\n" for @xmlfiles;
  9.        print "\n";
I'm guessing it's the sort. Would file::stat work better?
Thanks,
TEVO
Aug 20 '08 #1
3 3084
nithinpes
410 Recognized Expert Contributor
To sort the files based on modified time/date, you can make use of stat() function :
Expand|Select|Wrap|Line Numbers
  1. print "$_\n" foreach(sort {(stat($a))[9]<=>(stat($b))[9]} @xmlfiles);
  2.  
Aug 21 '08 #2
KevinADC
4,059 Recognized Expert Specialist
To sort the files based on modified time/date, you can make use of stat() function :
Expand|Select|Wrap|Line Numbers
  1. print "$_\n" foreach(sort {(stat($a))[9]<=>(stat($b))[9]} @xmlfiles);
  2.  
Yes, but that is not very efficient since it will have to stat() files more then one time. Better to build an array or hash of sort keys first, then sort the keys.
Aug 21 '08 #3
nithinpes
410 Recognized Expert Contributor
Yes, but that is not very efficient since it will have to stat() files more then one time. Better to build an array or hash of sort keys first, then sort the keys.
Agreed. Thanks for the update Kevin :)

Expand|Select|Wrap|Line Numbers
  1.  $stathash{$_} = (stat($_))[9] foreach(@xmlfiles);
  2.  print "$_\n" foreach(sort {$stathash{$a}<=>$stathash{$b}} keys %stathash);
  3.  
Aug 21 '08 #4

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

Similar topics

1
13639
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
32
2797
by: cat_dog_ass | last post by:
I am used to programming in DOS mode via Borland C++. I would now like to create programs for the Windows envirinment. Is it absoultely necessary to use Visual C++ to do this? Are there other tools that can help me in doing Windows programming via C++? How am I going to create drop-down menus and allow my programs to interact with the mouse? Regards, Icon
38
5183
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
4
6141
by: Stefan | last post by:
What sorting-algorithm is Array.Sort based upon? Is it an effective way of sorting an array? Regards, Stefan
3
1374
by: able | last post by:
Dear friends I have loaded a huge amount of data in an two domensional array. I need to do a lot searching in the array and sorting the array. My question is if an array is the most apropriate for this task? May be ArrayList or something else is a better object? Some adwise? Regards Able
1
2801
by: schmud | last post by:
i'm having trouble correctly sorting dates in my dataview. It will sort the column but it just sorts according to the month number. I need to display the data on my companies site with the most recent enteries first. Here is my code: Sub Page_Load(Source as Object, E as EventArgs) Dim ds As New DataSet ds.ReadXml(Server.MapPath("xml_files/news.xml")) Dim dv As New DataView(ds.Tables("text")) ...
9
2094
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt, etc. Within the procedure I also validate that the file loaded to the server was loaded within the current day. Everything has run smooth for quite some time using this method. Now the vendor has told me he has choice, but to the send the file...
2
1116
by: darrel | last post by:
We have a home grown web site. We store page information, including a time-stamp as to when it was last updated in a database table. We also store the site architecture (navigation) in an XML file. We want to create RSS feeds that show the most recent page updates for a particular node of our site menu. For instance, HR might want an RSS feed to list all recent HR page updates. To do this, we need to get the HR page, all childrend of...
6
2225
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to be echoed like a table into html. Almost everything goes well except for one entry going for an hour longer and one disappearing if shorter than 1hour (e.g. 30mins) <?php for($i=0; $i<$numRows; $i++) { $result =...
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.