473,769 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date comparison of files with if statements not working

154 New Member
Hi the purpose of the script I am trying to do is to access a dir and run the ls -l command which will show the files and the modified date, no w the script is to check two files in that dir , if the file has the modified month and day matching today print message log is fine else the log is not updated and thats it.

Now the script below I managed to parse to get both month and day for the files and the perl day and month

now the script shows successful when showing today but i changed the perl time to yesterday's date by hard coding a value and it still shows the log updated message when it suppose to show log not updated, so it is a problem i am guess ign with my else but i think i did it correct but i cannot seem to find where i am going wrong, could somebody help me out please.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #Purpose: Monitor script to monitor if logs is todays date
  3.  
  4. #Uncomment to use live perl time
  5. # @datepcs = split(/\s+/, scalar localtime);
  6. #    printf ("%s %s %s\n", @datepcs[1,2]);
  7.  
  8. #comment to remove hard coded value
  9. $dateTime ="Jul 26";
  10. print $dateTime;
  11.  
  12. chdir("/home/tibco/tibco/ACS_MQ_ADAPTER/logs");
  13. sleep 2;
  14. $test = "ls -l |";
  15. open (INFO, "$test") || die " Unable to run command $!";
  16. @rows = <INFO>;
  17.  
  18. open (TEST, ">/home/tibco/tibco/acs_monitor.txt") || die " Error could not open log $!";
  19. foreach (@rows) {
  20.  
  21.     if ($_ =~ m/MQJMSListener.log/) {
  22.         ###### Writing to file
  23.         print TEST "$_";
  24.         $_ = split;
  25.         $dates = "$_[5]";
  26.         $dates2 ="$_[6]";
  27.         print $dates . " $dates2";
  28.         #if ($dates . " $dates2" == "$dateTime") {
  29.         if ("$dateTime" == $dates . " $dates2") {
  30.             print "\n This $_[8] is fine \n";
  31.             print TEST '<span class="green">This log matches todays date</span><br>';
  32.         } else {
  33.             print "\n Dates do not match for $_[8] \n";
  34.             print TEST '<br><br><span class="red">This log does not match todays date</span><br>';
  35.         }
  36.     }
  37.  
  38.     if ($_ =~ m/EMSListener.log/) {
  39.         ###### Writing to file
  40.         print TEST "$_";
  41.         $_ = split;
  42.         $dates = "$_[5]";
  43.         $dates2 ="$_[6]";
  44.  
  45.         if ($dates . " $dates2" == "$dateTime") {
  46.             print "\n This $_[8] is fine \n";
  47.             print TEST '<span class="green">This log matches todays date</span><br>';
  48.         } else {
  49.             print "\n Dates do not match for $_[8] \n";
  50.             print TEST '<span class="red">This log does not match todays date</span><br>';
  51.         }
  52.     }
  53. }
  54. close(TEST);
  55. close(INFO);
  56. #END
  57.  
Jul 27 '07
12 3171
KevinADC
4,059 Recognized Expert Specialist
You can use the -M file operator which returns the date of a file in days since the program started. So if you wanted to see if files are matcing todays date you would see if the number returned by -M is less than one.

Expand|Select|Wrap|Line Numbers
  1. chdir('c:\\');
  2. @files = <*>;
  3. for (@files){
  4.    if ((-M) < 1) {
  5.        print $_,"\n";
  6.    }
  7. }
Jul 27 '07 #11
miller
1,089 Recognized Expert Top Contributor
I considered using that in the solution that I provided, which is why I included a link to the -X documentation. However, I figured a better introduction to a pure perl solution would closely match what he was already attempting to do. Namely match the current date, not just within 24 hours. Although I suspect that the range would actually be more favorably functionality.

- Miller
Jul 27 '07 #12
KevinADC
4,059 Recognized Expert Specialist
Certainly. My post is a complimentary extension of what you previously posted, at least I hope it is. ;)
Jul 27 '07 #13

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

Similar topics

2
2975
by: Daniel Fisher | last post by:
Hi All! I'm fairly new to PhP and basicly trying to learn right now. Now I have a problem - I have a fairly large collection of movies which people keep borrowing from me. And then not returning. So I've put together an Access database with three tables - my media library, a list of friends and email addresses, and a borrow table which uses foreign keys from the other two tables to see who has borrowed what. Borrow Table also has two...
6
10899
by: bissatch | last post by:
Hi, I am about to write an application that will display all pdf files in a folder and display them by file name in order of date. Is there an easy way of doing this? I was thinking about somehow adding filename and date created in an array, sorting by date and then printing onto an html page with a link to that file. How would I do this though? Is this the simplest method or is there a better way of doin this?
0
1562
by: Tim Brooks | last post by:
All, I'm hoping one of you Xml or Data gurus can offer an opinion. I'm working on an app to basically compare two semi-structured data files (e.g. Excel / CSV) to one another. But I need to compare them as if they were datatables... (ie a simple diff type tool won't work)... More specifically, the process I envision so far is: 1) Read in two files -- if Excel select appropriate sheet (would also require users to structure their...
6
8492
by: MarkAurit | last post by:
Im having difficulty coming up with a good algorithm to express the following comparison: "if <a given date> falls between the (current date - 5 days) and the (current date)" Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used for the inner and outer ranges, its how to express the "between" that has me. /* what Id like to do, in pseudo code */ dateToTest=DateTime.Parse("mm/dd/yy");
8
1379
by: JFB | last post by:
Hi All, I'm trying to find if a file name that includes the date is before todays to do some procedures. In my IF statement is working is just a few dates before but not is a year before. How can I fix this? Tks in advance... This is what I have: 'Set today's date
4
3765
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
7
3895
by: Brett_A | last post by:
I have the following code: If ad_expiration_date (date() + 90) then ad_expiration_date = (date() + 90) else end if What I want to happen is if the ad_expiration_date entered by the user is beyond 90 days from today's date, the Expiration Date should be today's date plus 90 days. If the entered date is less than 90 days
1
6320
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
16
4461
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after 22:02:51? (24 hour day is fine) How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is 10/01/08?
0
9589
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
9423
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
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7413
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
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
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
3
2815
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.