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
}
}
Code: Select all
TimeModified := "Apples" val "Apples"
Code: Select all
Msgbox %key%
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.