Jump to content
Sign in to follow this  
AustrianOak

calculating final day to delete ini key

Recommended Posts

AustrianOak

i want to delete an ini key depending on the date it was written and the date it was written is written in that very key so I need some help with math.

i set in my window in an updown control the data to '20'. this means that I want to delete all the keys older than 20 days.

but lets say my ini date format is like this: 7/7/07

how can i find the difference in days by using the variable set and the date written in the ini to determine if that specific key is older than 20 days.

i need serious help as i am horrible with calculations... :)

Share this post


Link to post
Share on other sites
k3v

_DateDiff will do this without having to figure out any math, but first you'll need to rearrange your date. Try stringsplit and stringreplace for this... (you probably already know all this stuff, but here's an example anyway...)

dim $cdate = iniread("date.ini","created","date","")

that's your date, fresh from the ini file in format 7/7/07 (of course, adjust to suit your ini file and key names)

dim $spldate = StringSplit($cdate,"/")

this is an array of the date components

$cdate = "";do this, or you'll have funky results.. or create a new variable for the rebuilt date
for $i =  $spldate[0] to 1 step -1 
    $cdate = $cdate & "/" & $spldate[$i]
Next

this reverses the date components... (I'm sure you can manipulate this code based on whether the format is M/D/YY or D/M/YY, I can't tell from your date... I think _DateDiff is using YYYY/M/D, so that's the format to match... the result looks funny, but we'll fix it next... ("/07/7/7")

$cdate = StringReplace($cdate,"/","20",1)

this does the replace for a count of ONE, so the "/" that's left over from the for...next loop above is replaced by the missing two YY characters... (in my testing, the _DateDiff function will need a YYYY year)...use your own logic to determin whether a 20 or a 19 should be appended here... sounds like this won't be a factor in your case.

The rest is easy...

dim $days = _DateDiff('d',$cdate,_nowcalc())

the 'd' is for days, and the result is an integer you can compare to your selected "days to delete" value... from your question, 20.

I'm happy to follow up if any of this needs clarification.

Otherwise, I hope it was helpful, and if there's a way to do this without all the reformatting, I hope someone can help you... (I'd kind-of like to know also.)

Edited by k3v

Share this post


Link to post
Share on other sites
AustrianOak

#Include <Date.au3>
dim $cdate = iniread(@ProgramFilesDir & "\temp.ini", "created","date","")
dim $spldate = StringSplit($cdate,"/")

$cdate = "";do this, or you'll have funky results.. or create a new variable for the rebuilt date
for $i =  $spldate[0] to 1 step -1
    $cdate = $cdate & "/" & $spldate[$i]
Next
$cdate = StringReplace($cdate,"/","20",1)
dim $days = _DateDiff('d',$cdate,_nowcalc())
msgbox(0, "", $days)

msgbox() should return the difference between the dates right? well its returning 0 everytime and i set my clock forward one day so its june 14 and the date written in my ini is june 13. should be returning 1 right?

:)

Edited by nowagain

Share this post


Link to post
Share on other sites
k3v

what does your ini file look like? I recreated your results by renaming my ini file so the script wouldn't find it... maybe your script isn't finding the key-value and has nothing to build a compare date from... ?

Another option... if the date value is the same as the ini file's create date you might also look at FileGetTime("file.ini",1,0) and build your compare date format from the array returned.

Share this post


Link to post
Share on other sites
AustrianOak

[Created]

Date=6/13/08

and no im pretty sure its finding it as i checked the $cdate var with msgbox()

Share this post


Link to post
Share on other sites
AustrianOak

reading throught the helpfile i found that _DateDiff is obviously failing because when it does it returns 0.

Edit:

If @error = "1" Then
    msgbox(0, "", "Invalid $sType")
ElseIf @error = "2" Then
    msgbox(0, "", "Invalid $sStartDate")
Else
    msgbox(0, "", "Invalid $sEndDate")
EndIf

I used that code above to determine what was failing and it looks like the $sEndDate is...

Second Edit:

Look here:

$sEndDate Input End date in the format "YYYY/MM/DD[ HH:MM:SS]"

I checked $cDate after the StringReplace() and it returned it in the format 2008/13/6 which seems to be YYY/DD/M

instead of in the format "YYYY/MM/DD[ HH:MM:SS]"

Edited by nowagain

Share this post


Link to post
Share on other sites
k3v

I checked $cDate after the StringReplace() and it returned it in the format 2008/13/6 which seems to be YYY/DD/M

instead of in the format "YYYY/MM/DD[ HH:MM:SS]"

I think I covered that in my first response.

(I'm sure you can manipulate this code based on whether the format is M/D/YY or D/M/YY, I can't tell from your date... I think _DateDiff is using YYYY/M/D, so that's the format to match...

Share this post


Link to post
Share on other sites
AustrianOak

I did manipulate the date to look like this: 2008/06/13

and it still throws a 0.

and i msgbox() after this line:

$cdate = StringReplace($cdate,"/","20",1)

and it says 2013/06/2008

its adding the '20' when it stringreplaced.

Edited by nowagain

Share this post


Link to post
Share on other sites
k3v

hmmm... it would help if I could see the code that's doing this... I tested my example before I posted it, and it worked fine...

In the mean time, instead of using a loop to manipulate to your desired format, try doing it a line at a time to reverse the MM & DD

$cdate = "7/7/07"

$spldate = StringSplit($cdate,"/")

$cdate = ""

$cdate = "20" & $spldate[3]

$cdate = $cdate & "/" & $spldate[1]

$cdate = $cdate & "/" & $spldate[2]

Share this post


Link to post
Share on other sites
AustrianOak

updated the code, so it looks like this:

#Include <Date.au3>

$cdate = iniread(@ProgramFilesDir & "\temp.ini", "created","Date","")
$spldate = StringSplit($cdate,"/")
$cdate = ""
$cdate = "20" & $spldate[3]
$cdate = $cdate & "/" & $spldate[1]
$cdate = $cdate & "/" & $spldate[2]
dim $days = _DateDiff('d',$cdate,_nowcalc())
msgbox(0, "", $days)

$cdate returns this: 202008/6/13

so its appending the 20 at the beginning for nothing...

Share this post


Link to post
Share on other sites
AustrianOak

as far as testing goes it works perfect like this:

#Include <Date.au3>

$cdate = iniread(@ProgramFilesDir & "\temp.ini", "created","Date","")
$spldate = StringSplit($cdate,"/")
$cdate = ""
$cdate = $spldate[3]
$cdate = $cdate & "/" & $spldate[1]
$cdate = $cdate & "/" & $spldate[2]
msgbox(0, "", $cdate)
dim $days = _DateDiff('d',$cdate,_nowcalc())
msgbox(0, "", $days)

do you see anything above that could possibly lead to future errors or problems?

Share this post


Link to post
Share on other sites
LostUser

reading throught the helpfile i found that _DateDiff is obviously failing because when it does it returns 0.

Edit:

If @error = "1" Then
    msgbox(0, "", "Invalid $sType")
ElseIf @error = "2" Then
    msgbox(0, "", "Invalid $sStartDate")
Else
    msgbox(0, "", "Invalid $sEndDate")
EndIf

I used that code above to determine what was failing and it looks like the $sEndDate is...

Second Edit:

Look here:

$sEndDate Input End date in the format "YYYY/MM/DD[ HH:MM:SS]"

I checked $cDate after the StringReplace() and it returned it in the format 2008/13/6 which seems to be YYY/DD/M

instead of in the format "YYYY/MM/DD[ HH:MM:SS]"

Did you get this working? If you are able to format your dates correctly (should be easy, just require a script to change them all). I tried this and it works.

include "date.au3"

$Date1="2008/06/13"
$Date2="2008/06/20"
$Date3="2009/06/13"

$FirstCheck=_DateDiff("d",$Date1,$Date2)
$SecondCheck=_DateDiff("d",$Date1,$Date3)


$msgDateDiff=$Date2 & "-" & $Date1 & "=" & $FirstCheck & @CRLF
$msgDateDiff=$msgDateDiff & $Date3 & "-" & $Date1 & "=" & $SecondCheck & @CRLF

MsgBox(0,"DateDiff check", $msgDateDiff)

I get 7 days and 365 days for the output.


Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Share this post


Link to post
Share on other sites
k3v

I guess my only response would be that the date you retrieved from the ini file isn't in the format you said, but in fact already has a four digit year, so in that case, of course adding a "20" would be pointless.

I'm just a little curious, however, why you would need to get the ini file's create date from a key value in that ini file, instead of getting the file's create date attrubute...

As for future errors or problems, this little piece of code doesn't throw any red flags at me... Glad you got it working... Good luck with your app.

Share this post


Link to post
Share on other sites
AustrianOak

I think my brain is working now.

I just realized that _NowCalc() writes the date in the same format _DateDiff() reads in so no need for all the calculations and splitting we were all struggling over. :)

correct me if im wrong.

Edited by nowagain

Share this post


Link to post
Share on other sites
k3v

but in this operation, _NowCalc() is only one argument of _DateDiff()... you still need a date to compare _NowCalc() with... that's the date you got from your ini file... which required reformatting to do the comparison.

Have you found another way?

Share this post


Link to post
Share on other sites
AustrianOak

you still need a date to compare _NowCalc()

yes, the I set this in the gui

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  

×