Jump to content

Get latest modified file within certain time period


 Share

Go to solution Solved by pixelsearch,

Recommended Posts

Hi All,

Newb to scripting and confused on how to call an array.
Based on help section, I saw that there are a way to get certain period by using array value in FileGetTime.

$filePath = "\\server\abc\"
$fileList = _FileListToArrayRec($filePath, "ABC_*_*", 1, 0, 0, 0)
_ArrayDisplay($fileList)

$gettime = FileGetTime($fileList, 1, 0)

IF ($gettime[3] >= 1 AND $gettime[3] <= 12) THEN
    MsgBox(0,"", "File created on " & $gettime[3] & $gettime[4] & $gettime[5], 10)
ELSE
    MsgBox(0,"","FAILED", 10)
ENDIF

But the Msgbox will not return the value I want and only display blank area.

Please advise.
Thank you in advance.

Link to comment
Share on other sites

  • Moderators

@Henwelcome to the forum. A couple of things to help you along, without giving away the fish:

  • You have to loop through the array one by one, calling FileGetTime on each file; you cannot just point it at the array as a whole. Something like this:
  • For $a = 1 To $fileList[0]
        $gettime = FileGetTime($fileList[$a], $FT_CREATED, $FT_ARRAY)
        ...
    Next

     

  • As for taking this info and determining the most recently created file, please search the forum as there are many solutions to this problem. Just one that might be of interest to you is Subz' solution in this (albeit older) thread:

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

9 hours ago, JLogan3o13 said:

@Henwelcome to the forum. A couple of things to help you along, without giving away the fish:

  • You have to loop through the array one by one, calling FileGetTime on each file; you cannot just point it at the array as a whole. Something like this:
  • For $a = 1 To $fileList[0]
        $gettime = FileGetTime($fileList[$a], $FT_CREATED, $FT_ARRAY)
        ...
    Next

     

  • As for taking this info and determining the most recently created file, please search the forum as there are many solutions to this problem. Just one that might be of interest to you is Subz' solution in this (albeit older) thread:

 

Hi JLogan3o13,

So I tried this with my script but it will return 0 value even though I got the latest modified file stored in the array.
The reason is because of "Subscript is a non-accesible varible" when I tried to put the condition IF, is there a way for me to call the array value in FileGetTime?

$filePath = "\\server\abc\"
$fileList = _FileListToArrayRec($filePath, "ABC_*_*", 1, 0, 0, 0)
_ArrayDisplay($fileList)

For $i = 1 To $fileList[0]
 $gettime = FileGetTime($fileList[$i], 1, 0)

 IF ($gettime[3] >= 1 AND $gettime[3] <= 12) THEN
     MsgBox(0,"", "File created on " & $gettime[3] & $gettime[4] & $gettime[5], 10)
 ELSE
     MsgBox(0,"","FAILED", 10)
 ENDIF
Next

 

Link to comment
Share on other sites

1 minute ago, Hen said:

Hi JLogan3o13,

So I tried this with my script but it will return 0 value even though I got the latest modified file stored in the array.
The reason is because of "Subscript is a non-accesible varible" when I tried to put the condition IF, is there a way for me to call the array value in FileGetTime?

$filePath = "\\server\abc\"
$fileList = _FileListToArrayRec($filePath, "ABC_*_*", 1, 0, 0, 0)
_ArrayDisplay($fileList)

For $i = 1 To $fileList[0]
 $gettime = FileGetTime($fileList[$i], 1, 0)

 IF ($gettime[3] >= 1 AND $gettime[3] <= 12) THEN
     MsgBox(0,"", "File created on " & $gettime[3] & $gettime[4] & $gettime[5], 10)
 ELSE
     MsgBox(0,"","FAILED", 10)
 ENDIF
Next

 

The array is a single dimension array containing six elements:
    $aArray[0] = year (four digits)
    $aArray[1] = month (range 01 - 12)
    $aArray[2] = day (range 01 - 31)
    $aArray[3] = hour (range 00 - 23)
    $aArray[4] = min (range 00 - 59)
    $aArray[5] = sec (range 00 - 59)

Link to comment
Share on other sites

  • Solution

Hey everybody :)
I'm having an issue with OP's question, something strange.

I tested his script on a folder named C:\Temp7 containing 4 files :
test part2.exe  (this 1st file got a creation time = 05:17:01 as shown in Windows Explorer)
vtest part1.a3x
vtest part1.au3
vtest part1.exe

If I run the following script (based on OP's script & JLogan3o13's suggestion), then it works fine for the 1st file...

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

$filePath = "C:\Temp7" ; or "C:\Temp7\"

$fileList = _FileListToArrayRec($filePath, "*", $FLTAR_FILES, $FLTAR_NORECUR , $FLTAR_NOSORT , $FLTAR_NOPATH)
; $fileList = _FileListToArrayRec($filePath, "*", $FLTAR_FILES, $FLTAR_NORECUR , $FLTAR_NOSORT , $FLTAR_RELPATH)
; $fileList = _FileListToArrayRec($filePath, "*", $FLTAR_FILES, $FLTAR_NORECUR , $FLTAR_NOSORT , $FLTAR_FULLPATH) ; (+++)

; $fileList = _FileListToArray($filePath, "*", $FLTA_FILES , False) ; False = no fullpath appended
; $fileList = _FileListToArray($filePath, "*", $FLTA_FILES , True) ; True = fullpath appended (+++)

_ArrayDisplay($fileList)

For $i = 1 To $fileList[0]
    $aGetTime = FileGetTime($fileList[$i], $FT_CREATED, $FT_ARRAY)
    ConsoleWrite("@error = " & @error & "   " & $fileList[$i] & @crlf)

    If $aGetTime[3] >= 1 And $aGetTime[3] <= 12 Then
        MsgBox(0, $fileList[$i], "File created (hhmmss) : " & $aGetTime[3] & $aGetTime[4] & $aGetTime[5])
    Else
        MsgBox(0, $fileList[$i], "Failed")
    EndIf
Next

FileGetTime.png.fab40e8e02a346f4fe17f6116ddea148.png

...then the scripts ends with an error. AutoIt Console :

@error = 0   test part2.exe
@error = 1   vtest part1.a3x

"script path here...294.au3" (24) : ==> Subscript used on non-accessible variable.:
If $aGetTime[3] >= 1 And $aGetTime[3] <= 12 Then
If $aGetTime^ ERROR
>Exit code: 1    Time: 3.944

The error seems easy to solve : the fullpath should be appended as last parameter in _FileListToArrayRec() or _FileListToArray(), but OP didn't append it, this created the issue.

This being said, an important question isn't answered : why FileGetTime() didn't throw an error for the 1st file ?
Nothing in the array shown in the pic above indicates to FileGetTime() where to find the 1st file, so how FileGetTime() did find the path of the 1st file ? And if it found the 1st file path, why not doing same for the 3 other files ?

* Please note that even if the 1st file has been processed by FileGetTime(), without @error, the  file creation time shown in the precedent pic isn't accurate, it should be 05h17m01s and not 05h16m53s as shown in the pic above. The accurate creation time would be displayed correctly only if we append the full path in the _FileListToArrayRec() or _FileListToArray() functions (tested).

* Could this strange behavior be related to the FindFirstFile windows API, its search handle and the dozen of remarks found on msdn ? FindFirstFile is used in both functions _FileListToArrayRec() and _FileListToArray()

It's interesting to note that there's never an issue with FileGetTime() when there's only 1 file to process, even if we make a "mistake" by not appending the full path in _FileListToArrayRec() or _FileListToArray() functions. As soon as there's a loop and several files are retrieved, then the issue always appears for the 2nd file processed by FileGetTime() and also @error = 1 for all the remaining files (when we don't indicate a full path as last parameter)

Let's hope I didn't miss something obvious. Maybe  it's a bug that should be reported : imho FileGetTime() shouldn't process the 1st file at all in the preceding script... but it does !

Link to comment
Share on other sites

1 hour ago, pixelsearch said:

...then the scripts ends with an error. AutoIt Console :

@error = 0   test part2.exe
@error = 1   vtest part1.a3x

"script path here...294.au3" (24) : ==> Subscript used on non-accessible variable.:
If $aGetTime[3] >= 1 And $aGetTime[3] <= 12 Then
If $aGetTime^ ERROR
>Exit code: 1    Time: 3.944

The error seems easy to solve : the fullpath should be appended as last parameter in _FileListToArrayRec() or _FileListToArray(), but OP didn't append it, this created the issue.

* Please note that even if the 1st file has been processed by FileGetTime(), without @error, the  file creation time shown in the precedent pic isn't accurate, it should be 05h17m01s and not 05h16m53s as shown in the pic above. The accurate creation time would be displayed correctly only if we append the full path in the _FileListToArrayRec() or _FileListToArray() functions (tested).

Hi pixelsearch,

Thank you so much for the point above, it is kinda mindblown for me to struggling (few days) in the array while the issue is on _FileListToArrayRec() ..

; $fileList = _FileListToArrayRec($filePath, "*", $FLTAR_FILES, $FLTAR_NORECUR , $FLTAR_NOSORT , $FLTAR_RELPATH)
; $fileList = _FileListToArrayRec($filePath, "*", $FLTAR_FILES, $FLTAR_NORECUR , $FLTAR_NOSORT , $FLTAR_FULLPATH) ; (+++)

; $fileList = _FileListToArray($filePath, "*", $FLTA_FILES , False) ; False = no fullpath appended
; $fileList = _FileListToArray($filePath, "*", $FLTA_FILES , True) ; True = fullpath appended (+++)

 

Link to comment
Share on other sites

9 hours ago, pixelsearch said:

why FileGetTime() didn't throw an error for the 1st file ?

As the script you posted above works if you put " FileChangeDir($filePath) " just before the For loop I would say that it's workingdir related
... obviously it is not an explanation :)

Link to comment
Share on other sites

Hi mikell,
Thanks for your input and test.

I just found a thread named "FileGetTime problem" where Geosoft talks about a file "really being held in memory" or "it's definitely the Windows cache". Though it's not the same situation, I was thinking too about the possibility of Windows using some infos from its cache (for the 1st file) in the script above.

Then googling on this subject show several posts (outside AutoIt too) where people have problems with all this.

Anyway, as it works really fine when the fullpath is included, I think we shouldn't spend too much time trying to understand, in this very case, why the 1st file is processed by FileGetTime() when no fullpath is indicated (and it's processed wrongly concerning the creation time) . Checking on @error should be a reflex in many cases.

Let Windows play with its cache as looong as it wishes :D

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...