A_LoopFileTimeModified in Associative Arrays

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wbm1113
Posts: 18
Joined: 28 Aug 2018, 11:19

A_LoopFileTimeModified in Associative Arrays

28 Aug 2018, 11:57

Disclaimer: I am a coding noob.

I have the following script that looks in a folder and grabs a copy of a file where the file name contains a specified string. If it finds more than one, it will grab the most recently modified one.

Code: Select all


	SummaryObject := Object()
	SummaryTimeCompare := Object()
	SummaryTimeCalculationArray := []
	SummaryChecker := 0		;initialize objects and set counter
	
	
	Loop, Files, %MatterFile%\*, R		;starts file loop (%MatterFile% comes from previous part of script)
	{
		SummaryChecker := InStr(A_LoopFileName, "summary")
		If(SummaryChecker>0) {
			SummaryObject[A_LoopFilePath] := A_LoopFileTimeModified		;if the word "summary" is found within the file, creates an associative array with the filepath and time modified.
		}		;The time modified of the file in question is 08/28/2018 at 10:35 am
	}
	
	
	If(SummaryChecker=0) {
		MsgBox, 16, Error, Could not find a summary within the file.		;skips if it doesn't find one
	} else {
	for key, val in SummaryObject	; (key = file path, val = time modified)
	{
		TimeModifiedDaysTotal := val
		TimeModified := val		; I set val to a couple other variables for clarity
		msgbox msg1: %timemodified%		; retrieves the above-referenced date, 20180828103502
		EnvSub, TimeModifiedDaysTotal, %A_Now%, Days	; gets the number of days between today and the date modified of the file retrieved
		SummaryTimeCompare[TimeModified] := TimeModifiedDaysTotal	; creates another array that associates the TimeModified value with the "days between" values
		msgbox msg2: %timemodified%		; again, the "TimeModified" value has not changed, it is 20180828103502
		SummaryTimeCalculationArray.Push(TimeModifiedDaysTotal) ; makes a simple array with only the "days between" values 
	}
	
	MostRecentDateNumberOfDays := Max(SummaryTimeCalculationArray*) ; gets the most recent date (my math is so backward with this, yes I am ashamed)
	
	for key, val in SummaryTimeCompare
	{
		msgbox msg3: %key%	%timemodified%		 ;here's where the problem is.  I set "TimeModified" to the Key of this array.  However, %key% = "-1223220402."  %timemodified% is still 20180828103502.
		If(MostRecentDateNumberOfDays=val) {	
			SummaryObjectComparer := key
		}
	}
	
	for key, val in SummaryObject
	{
		If(SummaryObjectComparer=val) {
			SummaryFilePath := key
		}
	}
	
I beat my head against the wall for about an hour and a half trying to figure out what the deal is. Ultimately, I did this:

Code: Select all

TimeModified := "Apples" val "Apples"
After I put the literal string "Apples" before and after the variable, then

Code: Select all

Msgbox %key%
would retrieve Apples20180828103502Apples.

Why? Where does that negative number even come from? It still shows up even if I delete the EnvSub line from the code. Also, for this particular instance of testing, the file loop only found one document containing the word "summary," so A_Index<2 for each "For" loop.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: A_LoopFileTimeModified in Associative Arrays

28 Aug 2018, 15:34

object keys in ahk32-bit are 32-bit signed ints. ure causing an integer overflow trying to store a key that big. either switch to ahk64-bit or store the key as a string:

Code: Select all

Arr := {}
TimeModified := 20180828103502
Arr[TimeModified ""] := "helloworld"

for key, value in Arr
	MsgBox % key "`n" value
wbm1113
Posts: 18
Joined: 28 Aug 2018, 11:19

Re: A_LoopFileTimeModified in Associative Arrays

28 Aug 2018, 20:43

Thank you!! It would have been a long, long time before I figured that out on my own.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Kodakku, Noesis and 379 guests