Dude, I tried of searching these thing in google
And what did you find? I simply don't believe there was nothing helpful in the results.
Maybe there was no example perl program that does exactly what you want, but enough to get you close.
Example: perl creation time file windows
Part of your problem is to fine the newest and oldest file. Let's try that: perl find newest file in directory. The first hit already explains how to get the newest or oldest file.
That's magic, isn't it? To me it also appears like magic that Google works for me, but not for you.
| [reply] |
Part of your problem is to fine the newest and oldest file. Let's try that: perl find newest file in directory. The first hit already explains how to get the newest or oldest file.
Curious as I am (because I've no clue how to find the 'newest' file, or even how to define what the 'newest' file is), I clicked on the link. I somewhat expected to find how to find the file which was modified last (which, BTW, is something totally different from "newest"). But not even that. For me, the first link explained how to find the largest file, for the second link, I couldn't even figure out what the question was, or whether there was an answer, and the third link wanted me to sign up before even revealing an answer. The fourth link tells me about the standard file tests in Perl, but Perl doesn't have a standard test for the age of a file. The fifth link (to Perlmonks) claims to find the oldest file in a directory, but what it really does is to find a file whose inode was last modified the longest time ago. The sixth link lists a couple of modules related to file manipulation - if the answer is in one of the listed modules, then it doesn't say so (but I doubt any of them does). After the sixth, I only scanned the titles of the pages, but none suggested to answer the question.
To me it also appears like magic that Google works for me, but not for you.
Your Google must be better than mine. Do you have the Platinum account?
| [reply] |
Curious as I am (because I've no clue how to find the 'newest' file, or even how to define what the 'newest' file is), I clicked on the link. I somewhat expected to find how to find the file which was modified last (which, BTW, is something totally different from "newest"). But not even that. For me, the first link explained how to find the largest file,
Well, this article does indeed talk about finding the largest file, and then at the end says
- To get the oldest file, use -M instead of -s.
- To get the smallest file, or the newest file, swap $a and $b.
So it explains how to sort files by date instead of size, and how to reverse the order. With a bit of thinking
it's not too hard to combine those.
Oh, and I just assumed that "last file" meant something like "file with the latest creation timestamp".
this article (first hit on my other google query) explains how to obtain a creation date for a file.
Combine the wisdom of these articles should solve the OP's problem.
Your Google must be better than mine. Do you have the Platinum account?
It's not Google that's different.
| [reply] |
JavaFan is right, these google links aren't just "plug it in" deals.
First, you need to decide if "creation time" is really what you want. That is the time that the file was created to begin with (and of course never changes) and is NOT the time the file was last modified.
When you create the file, the creation time and last modified times are the same, then they diverge every time file is updated. Almost always what is needed is the last modification time. On Windows NTFS, it is possible to get the creation time, but I think you'll have to go to the Win api to get it via a Perl function call. There is no standard Perl file test operator for creation and I don't even think it is available on Unix.
In the WinXP command shell type: "help DIR" and you will see that you can get that creation info, last accessed and last written. DIR drive:pathfilename [/A[:attributes]] /B /C /D /L /N
[/O[:sortorder]] /P /Q /S [/T[:timefield]] /W /X /4
So what you asked for, "creation" time is saved by the NT filesystem, but I don't think that is what you want.
Proceeding on the assumption that you want to use a combination of -f (plain file, not a directory) and -M modification date, then you need a strategy to arrive at what you want. .
-M is the age of file (at startup) in Days. You might get a number for a recent file like this: 1.86696759259259. That number is relative to the time that this Perl program is running. For your requirement, the absolute modification date doesn't appear to matter. So, run a sort on the -M file test (like the size example), pick first and last, then subtract those numbers. This difference will be expressed in days, so do some appropriate division by 24, 60 stuff to get this into say days:hours:minutes:seconds if that is what you need for your report. I mean 1.86696759259259 is a bit incomprehensible. Remember that a directory is a file, so you will need the -f file test also if you just want "plain files", which appears likely.
I hope I've given you a starting place for you to give this an attempt by yourself. So go at it. Its likely that some sort of problem will develop, but then you post your code, a simple example that demonstrates the problem and we go from there!
| [reply] |