Jump to content

Code to obtain only the first file of the a lot of folders


Go to solution Solved by ioa747,

Recommended Posts

Posted (edited)

I think that there are many types of first file.  I think you need to specify the type of sorting you require then we can determine the first file sorted in such a way.

Edited by Xandy
Link to comment
Share on other sites

yes you are absolutely right to complement my request I need to take the first file from each folder that has a .DNG extension

 

Link to comment
Share on other sites

9 hours ago, Netol said:

I need to take the first file from each folder that has a .DNG extension

Additional question  :

Are the files you are looking for located in one structure (a base directory and subdirectories), or are the directories spread all over the entire disk?

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Solution
Posted (edited)

example to take the first file with  a .txt extension

; https://www.autoitscript.com/forum/topic/211902-code-to-obtain-only-the-first-file-of-the-a-lot-of-folders/

#include <File.au3>

_FindAllDir(@MyDocumentsDir)

;----------------------------------------------------------------------------------------
Func _FindAllDir($dir)
    If StringRight($dir, 1) <> "\" Then $dir &= "\"
    $ArraySrtfiles = _FileListToArrayRec($dir, "*", $FLTAR_FOLDERS, $FLTAR_RECUR)
    If Not IsArray($ArraySrtfiles) Then
        ConsoleWrite($dir & " = Invalid input path" & @CRLF)
        Return
    Else
        Local $sResult
        For $x = 1 To $ArraySrtfiles[0]
            ;ConsoleWrite($dir & $ArraySrtfiles[$x] & @CRLF)
            $sResult = _FindFirstFile($dir & $ArraySrtfiles[$x], ".txt")
            If $sResult Then ConsoleWrite($dir & $ArraySrtfiles[$x] & $sResult & @CRLF)
        Next
    EndIf
EndFunc   ;==>_FindAllDir
;----------------------------------------------------------------------------------------
Func _FindFirstFile($dir, $Extension)
    ; Assign a Local variable the search handle of all files in the current directory.
    Local $hSearch = FileFindFirstFile($dir & "*" & $Extension)

    ; Check if the search was successful, if not display a message and return False.
    If $hSearch = -1 Then
        ;ConsoleWrite("! Error: No files/directories matched the search pattern." & @CRLF)
        Return False
    EndIf

    ; Assign a Local variable the empty string which will contain the files names found.
    Local $sFileName = ""

    While 1
        $sFileName = FileFindNextFile($hSearch)

        ; If there is no more file matching the search.
        If @error Then ExitLoop

        ; By first matching ExitLoop
        If $sFileName Then ExitLoop

    WEnd

    ; Close the search handle.
    FileClose($hSearch)

    Return $sFileName

EndFunc   ;==>_FindFirstFile
;----------------------------------------------------------------------------------------

 

Edited by ioa747
corection

I know that I know nothing

Link to comment
Share on other sites

13 hours ago, Xandy said:

Are we going to pull the first .DNG file sorted by name, date, or file size?

It would be very good if you brought the first file ordered by date and then name, the size is not important

Link to comment
Share on other sites

6 hours ago, ioa747 said:

example to take the first file with  a .txt extension

; https://www.autoitscript.com/forum/topic/211902-code-to-obtain-only-the-first-file-of-the-a-lot-of-folders/

#include <File.au3>

_FindAllDir(@MyDocumentsDir)

;----------------------------------------------------------------------------------------
Func _FindAllDir($dir)
    If StringRight($dir, 1) <> "\" Then $dir &= "\"
    $ArraySrtfiles = _FileListToArrayRec($dir, "*", $FLTAR_FOLDERS, $FLTAR_RECUR)
    If Not IsArray($ArraySrtfiles) Then
        ConsoleWrite($dir & " = Invalid input path" & @CRLF)
        Return
    Else
        Local $sResult
        For $x = 1 To $ArraySrtfiles[0]
            ;ConsoleWrite($dir & $ArraySrtfiles[$x] & @CRLF)
            $sResult = _FindFirstFile($dir & $ArraySrtfiles[$x], ".txt")
            If $sResult Then ConsoleWrite($dir & $sResult & @CRLF)
        Next
    EndIf
EndFunc   ;==>_FindAllDir
;----------------------------------------------------------------------------------------
Func _FindFirstFile($dir, $Extension)
    ; Assign a Local variable the search handle of all files in the current directory.
    Local $hSearch = FileFindFirstFile($dir & "*" & $Extension)

    ; Check if the search was successful, if not display a message and return False.
    If $hSearch = -1 Then
        ;ConsoleWrite("! Error: No files/directories matched the search pattern." & @CRLF)
        Return False
    EndIf

    ; Assign a Local variable the empty string which will contain the files names found.
    Local $sFileName = ""

    While 1
        $sFileName = FileFindNextFile($hSearch)

        ; If there is no more file matching the search.
        If @error Then ExitLoop

        ; By first matching ExitLoop
        If $sFileName Then ExitLoop

    WEnd

    ; Close the search handle.
    FileClose($hSearch)

    Return $sFileName

EndFunc   ;==>_FindFirstFile
;----------------------------------------------------------------------------------------

 

Thanks a lot my friend, with this code i can made my program.

best regards

Link to comment
Share on other sites

Posted (edited)

another approach

; https://www.autoitscript.com/forum/topic/211902-code-to-obtain-only-the-first-file-of-the-a-lot-of-folders/

#include <File.au3>

_FindAllDir(@MyDocumentsDir)

;----------------------------------------------------------------------------------------
Func _FindAllDir($dir)
    If StringRight($dir, 1) <> "\" Then $dir &= "\"
    $ArraySrtfiles = _FileListToArrayRec($dir, "*", $FLTAR_FOLDERS, $FLTAR_RECUR)
    If Not IsArray($ArraySrtfiles) Then
        ConsoleWrite($dir & " = Invalid input path" & @CRLF)
        Return
    Else
        Local $sResult
        For $x = 1 To $ArraySrtfiles[0]
            ;ConsoleWrite($dir & $ArraySrtfiles[$x] & @CRLF)
            $sResult = _FindAllFile($dir & $ArraySrtfiles[$x], "*.txt")
            If Not @error Then ConsoleWrite($sResult & @CRLF)
        Next
    EndIf
EndFunc   ;==>_FindAllDir
;----------------------------------------------------------------------------------------
Func _FindAllFile($dir, $FileMask)
    $ArraySrtfiles = _FileListToArrayRec($dir, $FileMask, $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT)
    If Not IsArray($ArraySrtfiles) Then
        Return SetError(1, 0, "")
    Else
        Local $sResult
        For $x = 1 To $ArraySrtfiles[0]
            $sResult = $dir & $ArraySrtfiles[$x]
            Return $sResult
        Next
    EndIf
EndFunc   ;==>_FindAllFile
;----------------------------------------------------------------------------------------

 

Edited by ioa747

I know that I know nothing

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