Baster Posted October 31, 2011 Share Posted October 31, 2011 (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 October 31, 2011 by Baster Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 Fixed it. But now it send the date 00/00/0000 Any other advice? Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 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 changeFileReadLine($file,8)toFileReadLine($file, 1)because now you try to read line 8. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 The file exists and i want to read the latest line so I tried FileReadLine($file,-1) too, but it doesn't still work! Link to comment Share on other sites More sharing options...
JohnOne Posted October 31, 2011 Share Posted October 31, 2011 "it doesn't still work!" is not really a lot to go on now is it? See if you can muster up a bit more than that. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 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 comment Share on other sites More sharing options...
czardas Posted October 31, 2011 Share Posted October 31, 2011 (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 October 31, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 (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 October 31, 2011 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 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"... Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 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. Link to comment Share on other sites More sharing options...
czardas Posted October 31, 2011 Share Posted October 31, 2011 (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 October 31, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 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? Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 (edited) ReplaceCase $OverTime $AnyDate=_DateTimeFormat(Stringformat(FileReadLine($file,8)),1) MsgBox(4096,"Today is",_Date_Time_SystemTimeToDateStr($AnyDate))in your script withCase $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 October 31, 2011 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 It says me that the difference is 0 days/months Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 What's the value of "Date read:"? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
czardas Posted October 31, 2011 Share Posted October 31, 2011 (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 October 31, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Baster Posted October 31, 2011 Author Share Posted October 31, 2011 Date read:// Link to comment Share on other sites More sharing options...
water Posted October 31, 2011 Share Posted October 31, 2011 Then the last line of your file is empty. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now