Jump to content

Problems with date in .txt files


Recommended Posts

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
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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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?

Link to post
Share on other sites

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
Link to post
Share on other sites

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

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
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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

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
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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...