How to create a file with specific date and time
Posted by hugo_owen (hugo_owen), 25 July 2005Like in Unix using the touch cmd I just want to create a file and be able to set the time attributes on that file - this will allow me to then search for any files newer than this one (in the last hour) to then replcate to another machine.
Posted by admin (Graham Ellis), 25 July 2005You're looking for the utime function. It differs from the Unix touch command in that it will not create the file for you in the first place - if the file does not exist, you should do that first (open for write then close is probably easiest).
To quote the manual ...
"[utime] Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERICAL access and modification times, in that order. Returns the number of files successfully changed. The inode change time of each file is set to the current time."
Example - timestamping a list of existing files to the current date and time, file names given on the command line:
$now = time;
utime $now, $now, @ARGV;
Posted by hugo_owen (hugo_owen), 26 July 2005Thanks Graham, I see what this does - effectivley touch'ing every file. But if I just want to change the date/time on a file. e.g have a file called test.gz and want to change it's date to 10am on the 25 july 2005.
Posted by admin (Graham Ellis), 26 July 2005Code:
If you think that timestamp is really ugly (and you would be right!) you might want to make up a timestamp using the timegm function in the Time::Local module that's standard with the Perl distribution. Here's an improved piece of code:
Note - this is GMT rather than local time (use timelocal if you need local time on the server).
Note - timegm month numbers go from 0 (January) to 11 (December) which may not be what you expect. Year numbers go from 0 to about 35 (for 2000 to 2035) and from about 38 to 99 (for 1938 to 1999)
Posted by hugo_owen (hugo_owen), 27 July 2005Thanks Graham for your quick responses, its much appreciated.
Excellent that works a treat - now all I need to do is to find files newer than this file like the unix "find" cmd:
find / -newer "file" -print
Posted by admin (Graham Ellis), 27 July 2005You might like to look at http://www.wellho.net/resources/ex.php4?item=p602/recur - a Perl example that recursively goes through a series or directories. Start to adapt it to your needs by using the -M operator on each of the objects traversed to see when it was last modified ...
Posted by hugo_owen (hugo_owen), 27 July 2005Thanks Graham, but the problem with -M and File::stat is it uses modified in days etc... the reason I want to create the file at a certian time and then find files newer than this file is so I can:
run the Perl script @ 10 o'clock, this then creates a file time stamped 9 o'clock, I can hopefully run find to find any files newer than this one to then cp to a mirror location. So effectively sync two machine every hour....
Posted by admin (Graham Ellis), 27 July 2005Those functions do indeed report a relative time ... but the stat function on which they're based calls up an absolute time. The great beauty of Perl is that if a function doesn't do quite what you want, there's very likely to be another that does. In this case, stat ...
Program to report on all files in or below a directory that have been modified since a started year/month/day hour/minute/second:
And here's what I got when I ran it ...
Comment by Dhaval (published 2010-10-25) Suggested link.
I like it very much. And i have one problem in coding in PERL language. I want to display dat and time dynamically. 1) Tue: 1:00 PM to 2:00 PM EST 2) Fri 11:00AM to 12:00PM EST every month recurring. So for that what i have to do?? [#3808]
You can Add a comment or ranking or edit your own comments
Average page ranking - 5.0
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho