Similar Threads:
1.compare last access date of a file in windows to current date
Hi all,
I am new to perl and am working on in a Windows 2000 enviroment. I
have the need to compare the last access time of a file to the system
date and if it is greater that 2 weeks prior to the system date write
that file to a text file that will be used to archive the file.
Any help would be appreciated.
Thanks
matt
2.URGENT HELP NEEDED--compare current date file with previous day
Hi All,
I am new in perl. I have report files which generate in particular
location day by day. I need to write a script to compare the current
date file with the previous day for first line. If there is a
difference it will generate email automatically to the users. The log
files with other few files generate in a folder which is named by date
(format: MM-DD-YY).
Some key points:
Location of the log files file:
/home/usr/09-26-06/log1.rpt and I need to compare with the previous
day: /home/usr/09-25-06/log1.rpt.... some thing like that.
The log.rpt file looks like:
PASS (P) 10
FAIL (F) 1.............................
I need to compare the second line. If the no. of FAIL changes then it
will send email automatically to the users.
Any help is appreciated.
Thanks.
3.--compare current date file with previous day
4.How compare two dates or convert date to number
Perl neophyte here.
I have a record date in the format mm/dd/yyyy and I want to compare it to
another date in the same format. Is there a simple way to accomplish this
task? All I want to do is say 'if date1 <= date0' do or don't do
something. I've looked, but to no avail. Help please...
Thank you in advance,
Hugh
5.perldoc -q 'date|time' FAQ -- and -- making same change to *lots* of files, *without* date changed
In article <dwH4f.29049$Io.3499@clgrps13>,
John W. Krahn < XXXX@XXXXX.COM > wrote:
>David Combs wrote:
...
>>
>> But doing that will screw up the creation dates of the
>> files, on which I rely to remind me what I've been
>> working on recently.
>
>AFAIK Unix doesn't record the creation dates of files anywhere.
Well, then whatever date it does change upon a "touch" of the file.
>
>
>> Since I want to make this change to just about EVERYTHING,
>> I think it best if the dates do NOT change.
>>
>> So, my question: how would *you* accomplish this task?
>
>man tar
>man cpio
John, how does that help me *edit* the gillion files?,
, all the while, *not* changing any of the dates/times associated
with the files?
I figured that if anyone could come up with a one-liner,
it would be *you*! :-)
>John
FYI: Here's what "perldoc -q 'date|time' >! foo.out" gets me:
Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq4.pod
How can I compare two dates and find the difference?
If you're storing your dates as epoch seconds then simply subtract one
from the other. If you've got a structured date (distinct year, day,
month, hour, minute, seconds values), then for reasons of accessibility,
simplicity, and efficiency, merely use either timelocal or timegm (from
the Time::Local module in the standard distribution) to reduce
structured dates to epoch seconds. However, if you don't know the
precise format of your dates, then you should probably use either of the
Date::Manip and Date::Calc modules from CPAN before you go hacking up
your own parsing routine to handle arbitrary date formats.
How do I find yesterday's date?
If you only need to find the date (and not the same time), you can use
the Date::Calc module.
use Date::Calc qw(Today Add_Delta_Days);
my @date = Add_Delta_Days( Today(), -1 );
print "@date\n";
Most people try to use the time rather than the calendar to figure out
dates, but that assumes that your days are twenty-four hours each. For
most people, there are two days a year when they aren't: the switch to
and from summer time throws this off. Russ Allbery offers this solution.
sub yesterday {
my $now = defined $_[0] ? $_[0] : time;
my $then = $now - 60 * 60 * 24;
my $ndst = (localtime $now)[8] > 0;
my $tdst = (localtime $then)[8] > 0;
$then - ($tdst - $ndst) * 60 * 60;
}
Should give you "this time yesterday" in seconds since epoch relative to
the first argument or the current time if no argument is given and
suitable for passing to localtime or whatever else you need to do with
it. $ndst is whether we're currently in daylight savings time; $tdst is
whether the point 24 hours ago was in daylight savings time. If $tdst
and $ndst are the same, a boundary wasn't crossed, and the correction
will subtract 0. If $tdst is 1 and $ndst is 0, subtract an hour more
from yesterday's time since we gained an extra hour while going off
daylight savings time. If $tdst is 0 and $ndst is 1, subtract a negative
hour (add an hour) to yesterday's time since we lost an hour.
All of this is because during those days when one switches off or onto
DST, a "day" isn't 24 hours long; it's either 23 or 25.
The explicit settings of $ndst and $tdst are necessary because localtime
only says it returns the system tm struct, and the system tm struct at
least on Solaris doesn't guarantee any particular positive value (like,
say, 1) for isdst, just a positive value. And that value can potentially
be negative, if DST information isn't available (this sub just treats
those cases like no DST).
Note that between 2am and 3am on the day after the time zone switches
off daylight savings time, the exact hour of "yesterday" corresponding
to the current hour is not clearly defined. Note also that if used
between 2am and 3am the day after the change to daylight savings time,
the result will be between 3am and 4am of the previous day; it's
arguable whether this is correct.
This sub does not attempt to deal with leap seconds (most things don't).
How do I validate input?
The answer to this question is usually a regular expression, perhaps
with auxiliary logic. See the more specific questions (numbers, mail
addresses, etc.) for details.
Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq5.pod
Why do I sometimes get an "Argument list too long" when I use <*>?
The "<>" operator performs a globbing operation (see above). In Perl
versions earlier than v5.6.0, the internal glob() operator forks csh(1)
to do the actual glob expansion, but csh can't handle more than 127
items and so gives the error message "Argument list too long". People
who installed tcsh as csh won't have this problem, but their users may
be surprised by it.
To get around this, either upgrade to Perl v5.6.0 or later, do the glob
yourself with readdir() and patterns, or use a module like File::KGlob,
one that doesn't use the shell to do globbing.
How do I randomly update a binary file?
If you're just trying to patch a binary, in many cases something as
simple as this works:
perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
However, if you have fixed sized records, then you might do something
more like this:
$RECSIZE = 220; # size of record, in bytes
$recno = 37; # which record to update
open(FH, "+<somewhere") || die "can't update somewhere: $!";
seek(FH, $recno * $RECSIZE, 0);
read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
# munge the record
seek(FH, -$RECSIZE, 1);
print FH $record;
close FH;
Locking and error checking are left as an exercise for the reader. Don't
forget them or you'll be quite sorry.
How do I get a file's timestamp in perl?
If you want to retrieve the time at which the file was last read,
written, or had its meta-data (owner, etc) changed, you use the -M, -A,
or -C file test operations as documented in perlfunc. These retrieve the
age of the file (measured against the start-time of your program) in
days as a floating point number. Some platforms may not have all of
these times. See perlport for details. To retrieve the "raw" time in
seconds since the epoch, you would call the stat function, then use
localtime(), gmtime(), or POSIX::strftime() to convert this into
human-readable form.
Here's an example:
$write_secs = (stat($file))[9];
printf "file %s updated at %s\n", $file,
scalar localtime($write_secs);
If you prefer something more legible, use the File::stat module (part of
the standard distribution in version 5.004 and later):
# error checking left as an exercise for reader.
use File::stat;
use Time::localtime;
$date_string = ctime(stat($file)->mtime);
print "file $file updated at $date_string\n";
The POSIX::strftime() approach has the benefit of being, in theory,
independent of the current locale. See perllocale for details.
How do I set a file's timestamp in perl?
You use the utime() function documented in "utime" in perlfunc. By way
of example, here's a little program that copies the read and write times
from its first argument to all the rest of them.
if (@ARGV < 2) {
die "usage: cptimes timestamp_file other_files ...\n";
}
$timestamp = shift;
($atime, $mtime) = (stat($timestamp))[8,9];
utime $atime, $mtime, @ARGV;
Error checking is, as usual, left as an exercise for the reader.
Note that utime() currently doesn't work correctly with Win95/NT ports.
A bug has been reported. Check it carefully before using utime() on
those platforms.
Found in /me-FIRST-in-PATH-bin/perl-5.8.6/lib/5.8.6/pod/perlfaq8.pod
How do I set the time and date?
Assuming you're running under sufficient permissions, you should be able
to set the system-wide date and time by running the date(1) program.
(There is no way to set the time and date on a per-process basis.) This
mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS
equivalent is "set time".
However, if all you want to do is change your time zone, you can
probably get away with setting an environment variable:
$ENV{TZ} = "MST7MDT"; # unixish
$ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
system "trn comp.lang.perl.misc";
How can I measure time under a second?
In general, you may not be able to. The Time::HiRes module (available
from CPAN, and starting from Perl 5.8 part of the standard distribution)
provides this functionality for some systems.
If your system supports both the syscall() function in Perl as well as a
system call like gettimeofday(2), then you may be able to do something
like this:
require 'sys/syscall.ph';
$TIMEVAL_T = "LL";
$done = $start = pack($TIMEVAL_T, ());
syscall(&SYS_gettimeofday, $start, 0) != -1
or die "gettimeofday: $!";
##########################
# DO YOUR OPERATION HERE #
##########################
syscall( &SYS_gettimeofday, $done, 0) != -1
or die "gettimeofday: $!";
@start = unpack($TIMEVAL_T, $start);
@done = unpack($TIMEVAL_T, $done);
# fix microseconds
for ($done[1], $start[1]) { $_ /= 1_000_000 }
$delta_time = sprintf "%.4f", ($done[0] + $done[1] )
-
($start[0] + $start[1] );
How do I timeout a slow event?
Use the alarm() function, probably in conjunction with a signal handler,
as documented in "Signals" in perlipc and the section on ``Signals'' in
the Camel. You may instead use the more flexible Sys::AlarmCall module
available from CPAN.
The alarm() function is not implemented on all versions of Windows.
Check the documentation for your specific version of Perl.
How do I add a directory to my include path (@INC) at runtime?
Here are the suggested ways of modifying your include path:
the PERLLIB environment variable
the PERL5LIB environment variable
the perl -Idir command line flag
the use lib pragma, as in
use lib "$ENV{HOME}/myown_perllib";
The latter is particularly useful because it knows about machine
dependent architectures. The lib.pm pragmatic module was first included
with the 5.002 release of Perl.
6. Current Date Variable
7. Comparing dates
8. compare date,time