Jump to content

Search Newest File


Recommended Posts

Hello

Been using this function for quite some time, but recently changed it to my latest project (install sql express 2008), it is runs but does not return the latest log file....

The log file thats generated from the installation will be unqiue on the time date you run it, reason for all the ????. This is also the case with the folder.

Help >_<

$file = ("Summary_"& @ComputerName &"_????????_??????_GlobalRules.txt")

$FindLog = _FileGetNewest("C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log", 0, 1, $file)
MsgBox(9,"Info", " Found : " & $FindLog[0])




;Function to Search all Sub Folders for a file
; ---------------------------------------
; Function _FileGetNewest()
;   Call with:    _FileGetNewest($sDirPath [, $iDateType [, $iRecurse [, $sPattern]]])
;   Where:    $sDirPath is the directory to search
;       $iDateType (0ptional) is the type of date to use:
;         -3 = (oldest) Modified
;         -2 = (oldest) Created
;         -1 = (oldest) Accessed
;         0 = Newest Modified (default)
;         1 = Newest Created
;         2 = Newest Accessed
;       $iRecurse (Optional): If non-zero causes the search to be recursive.
;         $sPattern (Optional): Wildcard pattern for matching
;   On success returns a 2-element array where:
;         [0] = the full path to the newest file found
;         [1] = the date/time stamp of the file "YYYYMMDDhhmmss" [from FileGetTime()]
;   On failure returns 0, sets @error and @extended (see code below).
;   Author: PsaltyDS
; ---------------------------------------
Func _FileGetNewest($sDirPath, $iDateType = 0, $iRecurse = 0, $sPattern = '*.*')
    Local $avNewest[2] = ["", 0]; [0] = file path, [1] = time stamp
    Local $flagNewest = True, $iModDateType = $iDateType
    Local $hFirst, $sFound, $iFileTime
    Local $hSubDirs, $sSubdir, $avFoundRecurse
    If StringRight($sDirPath, 1) <> '\' Then $sDirPath &= '\'
    If Not FileExists($sDirPath) Then Return SetError(1, 0, 0)
    If $iDateType < -3 Or $iDateType > 2 Then Return SetError(1, 1, 0)
    If $iDateType < 0 Then
        $iModDateType = $iDateType + 3
        $flagNewest = False
    EndIf
    ; Start search for files in this dir
    $hFirst = FileFindFirstFile($sDirPath & $sPattern)
    If ($hFirst <> -1) And (@error = 0) Then
        While 1
            $sFound = FileFindNextFile($hFirst)
            If @error Then ExitLoop
            $iFileTime = FileGetTime($sDirPath & $sFound, $iDateType, 1)
            If ($avNewest[1] = 0) Or _
                    (($flagNewest = True) And ($iFileTime > $avNewest[1])) Or _
                    (($flagNewest = False) And ($iFileTime < $avNewest[1])) Then
                $avNewest[0] = $sDirPath & $sFound
                $avNewest[1] = $iFileTime
            EndIf
        WEnd
    EndIf
    FileClose($hFirst)
    ; Search subdirectories, if $iRecurse is set
    If $iRecurse Then
        $hSubDirs = FileFindFirstFile($sDirPath & '*.*')
        If ($hSubDirs <> -1) And (@error = 0) Then
            While 1
                ; Start search for subdirectories
                $sSubdir = FileFindNextFile($hSubDirs)
                If @error = 0 Then
                    ; Only recurse to directories
                    If StringInStr(FileGetAttrib($sDirPath & $sSubdir), "D") Then
                        ; Search a subdirectory
                        $avFoundRecurse = _FileGetNewest($sDirPath & $sSubdir, $iDateType, $iRecurse, $sPattern)
                        If @error Then
                            ; Nothing found
                            ContinueLoop
                        Else
                            ; Check if newest
                            If $avFoundRecurse[1] > $avNewest[1] Then $avNewest = $avFoundRecurse
                            If ($avNewest[1] = 0) Or _
                                    (($flagNewest = True) And ($avFoundRecurse[1] > $avNewest[1])) Or _
                                    (($flagNewest = False) And ($avFoundRecurse[1] < $avNewest[1])) Then
                                $avNewest = $avFoundRecurse
                            EndIf
                        EndIf
                    EndIf
                Else
                    ; Done looking for subdirs
                    ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    If $avNewest[1] = 0 Then
        Return SetError(2, 0, 0); None found
    Else
        Return $avNewest
    EndIf
EndFunc   ;==>_FileGetNewest
Link to comment
Share on other sites

  • Moderators

Scriptonize,

FileFindFirstFile returns a search handle - you have to use FileFindNextFile to get all the file names within that handle.

Alpinestar,

PsaltyDS' code returns the correct file for me - as I expected. As the function works, and your syntax seems correct, my only thought is that your pattern (as defined in $file) is not matching - are you sure you have that right? I hesitate to ask, but that is all I can think of!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Scriptonize,

FileFindFirstFile returns a search handle - you have to use FileFindNextFile to get all the file names within that handle.

Alpinestar,

PsaltyDS' code returns the correct file for me - as I expected. As the function works, and your syntax seems correct, my only thought is that your pattern (as defined in $file) is not matching - are you sure you have that right? I hesitate to ask, but that is all I can think of!

M23

You are right.

I figured that out just after posting in here.

Thank you. |-)

@AlpineStar

When I change the line:

MsgBox(9,"Info", " Found : " & $FindLog[0])

into.....

MsgBox(0,"Info", " Found : " & $FindLog[0])

I have a positive result as wel.

If you learn from It, it's not a mistake

Link to comment
Share on other sites

  • Moderators

Scriptonize,

I think you have found the answer! >_<

It seems MsgBox will just fail with an invalid flag, without any error message.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...