Jump to content
Sign in to follow this  
Home Bunny

FileSearch - By Date

Recommended Posts

Home Bunny

Hi,

Is there an alternative to 'FileFindFirstFile',

that can handle only files created after a specified date ?

$Search = FileFindFirstFile($SourceFolder & "\*.chp")

Running trough 8700 files with 'FileFindFirstFile' takes 4 minutes, and there are only 5 relevant files ...

Regards,

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

try this

#include<Array.au3>
$sPath = @ScriptDir ; der Ordner, der durchsucht werden soll
$sExt = '.au3' ; Dateiendung der gewünschten Dateien (z.B. alle Txt-Dateien)
$sDate = '20090505000000' ; Alle Dateien die neuer sind als dieses Datum/Uhrzeit (Format: YYYYMMDDhhmmss)

$array = _GetFilesFolder_Rekursiv($sPath, $sExt, 0, $sDate)
;~ $array = _GetFilesFolder_Rekursiv($sPath, $sExt, 0, -1)

_ArrayDisplay($array)

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [,$sDate=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     Rekursive Auflistung von Dateien und/oder Ordnern
; Parameter(s):    $sPath     der Basispfad für die Auflistung ('.' -aktueller Pfad, '..' -Parentpfad)
;      optional:   $sExt      Erweiterung für Dateiauswahl '*' oder -1 für alle (Standard)
;      optional:   $iDir      -1 Dateien+Ordner(Standard), 0 nur Dateien, 1 nur Ordner
;      optional:   $sDate     gibt nur Dateien/Ordner zurück, die neuer sind als das angegebene Datum
;      optional:   $iRetType  0 gibt Array, 1 gibt String zurück
;      optional:   $sDelim    legt Trennzeichen für Stringrückgabe fest
;                             0 -@CRLF (Standard); 1 -@CR; 2 -@LF; oder beliebiges Zeichen
; Return Value(s): Array (Standard) od. String mit den gefundenen Pfaden der Dateien und/oder Ordner
;                  Array[0] enthält die Anzahl der gefundenen Dateien/Ordner
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt = '*', $iDir = -1, $sDate = -1, $iRetType = 0, $sDelim = '0')
    If StringLen($sDate) <> 14 Then $sDate = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath), $sExt, $iDir, $sDate, $sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, 1)
    EndIf
EndFunc   ;==>_GetFilesFolder_Rekursiv

Func _ShowSubFolders($Folder, $Ext = '*', $Dir = -1, $sDate = -1, $Delim = @CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $file.DateLastModified > $sDate Then
                If $Ext <> '*' Then
                    If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                            $strFiles &= $file.Path & $Delim
                Else
                    $strFiles &= $file.Path & $Delim
                EndIf
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If $Subfolder.DateLastModified > $sDate Then
            If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        EndIf
        _ShowSubFolders($Subfolder, $Ext, $Dir, $sDate, $Delim)
    Next
EndFunc   ;==>_ShowSubFoldersoÝ÷ Úí+ºÚ"µÍÚ[ÛYH    Ð^K]LÉÝÂÚ[ÛYH Ñ]K]LÉÝÂÚ[ÛYH Ñ[K]LÉÝÂÔÛÝÛÌÍÜÛÝÙQÛHØÜ    [È ÌÎNÉÌLÉÌÎNÂÑØ][È[È[^BÌÍÙ[SÝHÑ[SÝÐ^J  ÌÍÜÛÝÙQÛ ][ÝÊ][ÝËJB[H    ÌÍÙÝ[ÌWBÓÛÜÝYÚ^BÜ    ÌÍÖHHÈ  ÌÍÙ[SÝÌBÔ]Y]HÜX][Û[YHÙ[B   ÌÍÑ]HH[QÙ][YJ   ÌÍÜÛÝÙQÛ [È ][ÝÉÌLÉ][ÝÈ   [È ÌÍÙ[SÝÉÌÍÖKK
BÑÜX]]HÜÙHÚ]]HQ    ÌÍÙ]HHÝ[ÑÜX]
    ][ÝÉËÉËÉÈ    ÎÎÉ][ÝË    ÌÍÑ]VÌK ÌÍÑ]VÌWK    ÌÍÑ]VÌK ÌÍÑ]VÌ×K   ÌÍÑ]VÍK ÌÍÑ]VÍWJBÐØ[Ý[]HYÙK[[ÝH[ÈÛ[Ù][^ÂYÑ]QY ÌÎNÙ ÌÎNË ÌÍÙ]KÓÝÐØ[Ê
JH  È
È[ÈH[YBÑ[Q[]J    ÌÍÜÛÝÙQÛ [È ][ÝÉÌLÉ][ÝÈ   [È ÌÍÙ[SÝÉÌÍÖJBÐ^PY
    ÌÍÙÝ[   ÌÍÜÛÝÙQÛ [È ÌÍÙ[SÝÉÌÍÖJBÓÙÐÞ
K   ][ÝÑ[È[]Y][ÝË  ÌÍÙ[SÝÉÌÍÖKJB[Y ÌÍÙÝ[ÌHHPÝ[
    ÌÍÙÝ[
B^Ð^^J  ÌÍÙÝ[

Mega

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
SmOke_N

Weeeeeeeee.

I don't know if 300 ms is suffice for you with 9000 files, but I gave it a shot :)

#include <array.au3>
#include <date.au3>

Global $a_less_than = _File_GetByDate(@HomeDrive & "\AU3", "*.au3", "2009/05/01", 0, 1, 0, 1)
_ArrayDisplay($a_less_than, "Less than")
Global $a_equal_to = _File_GetByDate(@HomeDrive & "\AU3", "*.au3", "2009/05/01", 1, 1, 0, 1)
_ArrayDisplay($a_equal_to, "Equal to")
Global $a_greater_than = _File_GetByDate(@HomeDrive & "\AU3", "*.au3", "2009/05/01", 2, 1, 0, 1)
_ArrayDisplay($a_greater_than, "Greater than")

;~ $s_date: YYYY/MM/DD [HH:MM:SS] format
;~ $i_less_equal_greater: 0=less, 1=equal, 2=greater
;~ $i_last_created_accessed_written: 0=modified, 1= created, 2=accessed
;~ $i_file_type: 0=All file types, 1=Files only, 2=Directories only
Func _File_GetByDate($s_dir, $s_mask, $s_date, $i_less_equal_greater = 0, $i_last_created_accessed_written = 1, $i_file_type = 0, $i_descending = 0, $f_fullpath = True)
    If StringRegExp($s_date, "\d{4}/\d\d/\d\d") = 0 Then Return SetError(1, 0, 0)
    
    Local $a_files = _File_ListByTimeArray($s_dir, $s_mask, $i_last_created_accessed_written, $i_file_type, $i_descending, $f_fullpath)
    If @error Then Return SetError(2, 0, 0)
    
    Local $f_zero_time = False
    If StringRegExp($s_date, "\d{4}/\d\d/\d\d \d+:\d+:\d+") = 0 Then
        $s_date &= " 00:00:00"
        $f_zero_time = True
    EndIf
    
    Local $a_ret[$a_files[0] + 1][2]
    
    Local $i_diff, $s_format,  $f_add = False
    Local $a_date, $f_found = 0, $i_cc = 0
    
    For $i = 1 To $a_files[0]
        $a_date = FileGetTime($a_files[$i], $i_last_created_accessed_written)
        If @error Then ContinueLoop
        $s_format = StringFormat("%s/%s/%s %s:%s:%s", _
            $a_date[0], $a_date[1], $a_date[2], $a_date[3], $a_date[4], $a_date[5])
            
        If $f_zero_time Then
            $i_diff = Int(_DateDiff("d", _
                $a_date[0] & "/" & $a_date[1] & "/" & $a_date[2] & " 00:00:00", $s_date))
        Else
            $i_diff = Int(_DateDiff("d", $s_format, $s_date))
        EndIf
        
        If $i_less_equal_greater = 2 And $i_diff < 0 Then
            If $f_found = 0 Then $f_found = 1
            $f_add = True
        ElseIf $i_less_equal_greater = 0 And $i_diff > 0 Then
            If $f_found = 0 Then $f_found = 1
            $f_add = True
        ElseIf $i_less_equal_greater = 1 And $i_diff = 0 Then
            If $f_found = 0 Then $f_found = 1
            $f_add = True
        ElseIf $f_found Then
            ExitLoop
        EndIf
        
        If $f_add Then
            $i_cc += 1
            $a_ret[$i_cc][0] = $a_files[$i]
            $a_ret[$i_cc][1] = $s_format
        EndIf
        $f_add = False
    Next
    
    If $i_cc = 0 Then Return SetError(3, 0, 0)
    
    ReDim $a_ret[$i_cc + 1][2]
    $a_ret[0][0] = $i_cc
    Return $a_ret
EndFunc

;~ $i_last_created_accessed_written: 0=modified, 1= created, 2=accessed
;~ $i_file_type: 0=All file types, 1=Files only, 2=Directories only
Func _File_ListByTimeArray($s_dir, $s_mask = "*", $i_last_created_accessed_written = 1, $i_file_type = 0, $i_descending = 0, $f_fullpath = True)
    $s_dir = StringRegExpReplace($s_dir, "[\\/]+\z", "") & "\"
    
    Local $s_switch = "/T:C", $s_full_path = "/s "
    Switch $i_last_created_accessed_written
        Case 2
            $s_switch = "/T:A"
        Case 3
            $s_switch = "/T:W"
    EndSwitch
    
    If Not $f_fullpath Then $s_full_path = ""
    
    Local $s_file_type = ""
    If $i_file_type = 1 Then
        $s_file_type = "/a:-d "
    ElseIf $i_file_type = 2 Then
        $s_file_type = "/a:d "
    EndIf
    
    Local $s_descending = "/o:d "
    If $i_descending Then $s_descending = "/o:-d "
    
    Local $s_files = ""
    Local $i_pid = Run(@ComSpec & _
        " /c dir /b " & $s_full_path & $s_file_type & $s_descending & $s_switch & _
        ' "' & $s_dir & $s_mask & '"', "", @SW_HIDE, 6)
    
    While 1
        $s_files &= StdoutRead($i_pid)
        If @error Then ExitLoop
    WEnd
    
    If Not $s_files Then Return SetError(1, 0, 0)
    
    $s_files = StringRegExpReplace($s_files, "\r|\n+\z", "")
    Return StringSplit($s_files, @LF)
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Home Bunny

Thx SmOke_N & Xenobiologist for your prompt repleys.

altough the script off SmOke_N worked faster, i've used the coding off Xenobiologist (top one) as this worked recurs.

Timings for 8700 files with 35 files the last 30 days:

FileFindFirstFile: 3 min

_GetFilesFolder_Rekursiv: 1 min

Thanks !!!!!!!

Share this post


Link to post
Share on other sites
SmOke_N

Thx SmOke_N & Xenobiologist for your prompt repleys.

altough the script off SmOke_N worked faster, i've used the coding off Xenobiologist (top one) as this worked recurs.

Timings for 8700 files with 35 files the last 30 days:

FileFindFirstFile: 3 min

_GetFilesFolder_Rekursiv: 1 min

Thanks !!!!!!!

:) Um, mine is recursive :party:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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  

×