Sign in to follow this  
Followers 0
Baster

Problems with date in .txt files

26 posts in this topic

#1 ·  Posted (edited)

Anyone of you can help me with my terrible problem?

I want to read a date from a line in a .txt file. I've tried a lot but i didn't manage it!

In that line there is only the date, for example 25/10/2011. I made this but it doesn't work!

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <Date.au3>

#include <File.au3>

#region ### START Variable Declaration ### Variables=

$file=FileOpen("C:Users\Utente\Documents\Date.txt")

$TodayDate=_Date_Time_GetLocalTime()

#EndRegion ### END Variable Declaration section ###

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Territories", 477, 237, 192, 124)

$File = GUICtrlCreateMenu("&File")

$Exit = GUICtrlCreateMenuItem("E&xit", $File)

$Tools = GUICtrlCreateMenu("T&ools")

$OverTime = GUICtrlCreateMenuItem("Ch&eck expiration", $Tools)

GUISetState(@SW_SHOW)

Dim $Form1_AccelTable[2][2] = [["!x", $Exit],["^h", $OverTime]]

GUISetAccelerators($Form1_AccelTable)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Exit

Exit

Case $OverTime

$AnyDate=_DateTimeFormat(Stringformat(FileReadLine($file,8)),1)

MsgBox(4096,"Today is",_Date_Time_SystemTimeToDateStr($AnyDate))

EndSwitch

WEnd

Could someone help me? Please..

Edited by Baster

Share this post


Link to post
Share on other sites



Your FileOpen statement is wrong. Insert a backslash after the drive.

$file=FileOpen("C:\Users\Utente\Documents\Date.txt")


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Fixed it. But now it send the date 00/00/0000

Any other advice?

Share this post


Link to post
Share on other sites

Please insert this line at the top of your script:

MsgBox(0,"", "File exists: " & FileExists("C:\Users\Utente\Documents\Date.txt"))
If you don't get 1 as a result then the input file does not exist.

And you should change

FileReadLine($file,8)
to
FileReadLine($file, 1)
because now you try to read line 8.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The file exists and i want to read the latest line so I tried

FileReadLine($file,-1)

too, but it doesn't still work!

Share this post


Link to post
Share on other sites

The message box shows as title: "Today is" and the text in it is:"00/00/0000"

Is the problem about the date caused by a mistake in the input? In the .txt file the date is 28/10/2011 without the time, can you tell me if i have to write it too? Or the order is wrong? I mean, i wrote the date following DD/MM/YYYY form, did I write right? Or the date must be MM/DD/YYYY?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Okay this boils down to too many possible causes for error. Deal with one problem at a time. Firstly can you read the line? Straight from the help file (more or less).

$file = FileOpen("test.txt", 0)
; Check if file opened for reading OK
 
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
 
; Read last line of text
$line = FileReadLine($file, -1)
If @error Then $line = "Error reading line"
FileClose($file)
 
MsgBox(0, "Line read:", $line)

Yes I can read the line, now what?

Edited by czardas

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

These 2 lines

$AnyDate=_DateTimeFormat(Stringformat(FileReadLine($file,8)),1)
MsgBox(4096,"Today is",_Date_Time_SystemTimeToDateStr($AnyDate))
are wrong as can be.

You use Stringformat wrong because you don't specify a format control.

You use _Date_Time_SystemTimeToDateStr wrong because $AnyDate isn't in the right format. It has to be a $tagSYSTEMTIME structure.

Please tell us what you try to achieve and we might help you with your script.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Perfect! It Works! It reads the line and sends "Line read:" and "28/10/2011"! I've put it in the code but it still sends me "00/00/0000"...

Share this post


Link to post
Share on other sites

I want to read the latest line of a .txt file and compare it with the current date to check if data stored in it is 2 months old.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I'm sure there are many ways to do something like this. I think you should use _DateDiff => look in the help file. I don't know how all the date functions work but this may be a possible solution.

#include <Date.au3>
 
Dim $line = "28/7/2011"
 
Dim $aDate = StringSplit($line, "/", 2) ; Splits the date
Dim $sStartDate = $aDate[2] &"/"& $aDate[1] &"/"& $aDate[0] ; Reverse to get Year Month Day format
Dim $sEndDate = @YEAR &"/"& @MON &"/"& @MDAY ; Current date
 
Dim $iMonthDiff = _DateDiff("M", $sStartDate, $sEndDate) ; Get the difference
MsgBox(0, "Difference in Months:", $iMonthDiff)

You might want to get the difference in days instead of months, and set 60 days as the limit.

Edited by czardas

Share this post


Link to post
Share on other sites

To display the number of months between the date in the file and today I would use:

$AnyDate = StringMid($AnyDate, 7, 4) & "/" & StringMid($AnyDate, 4, 2) & "/" & StringMid($AnyDate, 4, 2)
MsgBox(4096, "",  "Date read: " & $AnyDate & @CRLF & _
    "Today: " & _NowCalcDate() & @CRLF & _
    "Difference in days: " & _DateDiff("d", $AnyDate, _NowCalcDate()) & @CRLF _
    "Difference in months: " & _DateDiff("m", $AnyDate, _NowCalcDate()))

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

It works alone but in my program it doesn't. Do you think it's because the line is not in string format? Or something similar?

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Replace

Case $OverTime
  $AnyDate=_DateTimeFormat(Stringformat(FileReadLine($file,8)),1)
  MsgBox(4096,"Today is",_Date_Time_SystemTimeToDateStr($AnyDate))
in your script with
Case $OverTime
  $AnyDate = FileReadLine($file,-1) ; reads the last line of the file
  $AnyDate = StringMid($AnyDate, 7, 4) & "/" & StringMid($AnyDate, 4, 2) & "/" & StringMid($AnyDate, 4, 2)
  MsgBox(4096, "",  "Date read: " & $AnyDate & @CRLF & _
     "Today: " & _NowCalcDate() & @CRLF & _
     "Difference in days: " & _DateDiff("D", $AnyDate, _NowCalcDate()) & @CRLF _
     "Difference in months: " & _DateDiff("M", $AnyDate, _NowCalcDate()))

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

It says me that the difference is 0 days/months

Share this post


Link to post
Share on other sites

What's the value of "Date read:"?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I spotted one or tow errors in Water's code, which I have fixed. Also if you use StringMid, instead of StringSplit, make sure the date always contains leading zeros => 01/01/2011.

$AnyDate = "28/07/2011"
 
$AnyDate = StringMid($AnyDate, 7, 4) & "/" & StringMid($AnyDate, 4, 2) & "/" & StringMid($AnyDate, 1, 2)
 
MsgBox(4096, "",  "Date read: " & $AnyDate & @CRLF & _
    "Today: " & _NowCalcDate() & @CRLF & _
    "Difference in days: " & _DateDiff("d", $AnyDate, _NowCalcDate()) & @CRLF & _
    "Difference in months: " & _DateDiff("m", $AnyDate, _NowCalcDate()))
Edited by czardas

Share this post


Link to post
Share on other sites

Date read://

Share this post


Link to post
Share on other sites

Then the last line of your file is empty.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0