Jump to content

FileFindFirst/NextFile and Gui


Recommended Posts

I am trying to understand FileFindFirst/NextFile, by trying to get a listview GUI of the filenames of a directory

Because the Gui And the FileFind both need a loop to function, how do you incorporate the 2 together?

The helpfile example has no GUI

And trying to Sync it with AdLibEnable() doesn't seem to work either

Is there any other function I can use to get all Filenames within a directory

Ty as always

Paulie

Link to comment
Share on other sites

The GUI loops and the FindNextFile loops are separate. See the code below I just did for a combo box. Listbox should be the same.

#include <GUIConstants.au3>

GUICreate("file list",220,250, 100,400,-1,$WS_EX_ACCEPTFILES)

$combo=GUICtrlCreateCombo("",10, 10, 140,230)

FileChangeDir("C:\Mark")

$search = FileFindFirstFile("*.*")

$filestr="|"

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$filestr = $filestr & FileFindNextFile($search) & "|"

If @error Then ExitLoop

WEnd

$filestr=StringTrimLeft($filestr,5)

$filestr=StringTrimRight($filestr,1)

$filearray=StringSplit($filestr,"|")

GUICtrlSetData($combo,$filestr,$filearray[2])

FileClose($search)

GUISetState(@SW_SHOW)

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

EndSelect

WEnd

Exit

Link to comment
Share on other sites

The GUI loops and the FindNextFile loops are separate. See the code below I just did for a combo box. Listbox should be the same.

#include <GUIConstants.au3>
GUICreate("file list",220,250, 100,400,-1,$WS_EX_ACCEPTFILES)
$combo=GUICtrlCreateCombo("",10, 10, 140,230)
FileChangeDir("C:\Mark")
$search = FileFindFirstFile("*.*")  
$filestr="|"
; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $filestr = $filestr & FileFindNextFile($search) & "|" 
    If @error Then ExitLoop
WEnd
$filestr=StringTrimLeft($filestr,5)
$filestr=StringTrimRight($filestr,1)
$filearray=StringSplit($filestr,"|")
GUICtrlSetData($combo,$filestr,$filearray[2])
FileClose($search)
GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
WEnd
Exit
Ahh... I see now

Geez that was pretty obvious :"> :">

Probably shouldn't try to problem solve at 1:00AM

Thanks

Link to comment
Share on other sites

Lesson #6 from "Welcome to Autoit 1-2-3"

; includes
#include <GuiConstants.au3>
#include <file.au3>

; create the GUI.
$win = GUICreate("File List/View Demo", 614, 370)
; set the font for the GUI
GUISetFont(9, 400, -1, "MS Sans Serif")
; create buttons.
$btnList = GUICtrlCreateButton("&List Files", 10, 330, 75, 25)
$btnView = GUICtrlCreateButton("&View File", 85, 330, 75, 25)
; create the left list.
$TutorItList = GUICtrlCreateList("", 10, 10, 150, 330)
; create the right edit.
$TutorItEdit = GUICtrlCreateEdit("Please select a tutorial from the list to your left.", 175, 10, 420, 345, $ES_AUTOVSCROLL + $ES_READONLY + $ES_MULTILINE + $WS_VSCROLL)
; set the edit colors.
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x000000)
; set focus to the edit.
GUICtrlSetState($TutorItList, $GUI_FOCUS)
; show the GUI.
GUISetState()

; start the loop.
While 1
    ; listen for a message
    $msg = GUIGetMsg()
    ; using select/case for the message
    Select
        Case $msg = $GUI_EVENT_CLOSE 
            Exit
        Case $msg = $btnList
            Set_tutor()
        Case $msg = $btnView
            View_tutor()
    ; end the selections        
    EndSelect
    
WEnd

; Function to populate the left list.
Func Set_tutor()
    $TutList = _FileListToArray (@HomeDrive & "\", "*.txt", 1) ; list files to an array.
    If (Not IsArray($TutList)) Or (@error = 1) Then
        MsgBox(262208, "Tutor Error", "No Files\Folders Found.   ", 5)
        Return
    EndIf
    GUICtrlSetData($TutorItList, "") ; set list to empty.
    For $x = 1 To $TutList[0] ; for loop to place the files in the list.
        GUICtrlSetData($TutorItList, (StringTrimRight($TutList[$x], 4)) & "|", 1) ; string trim the last 4 characters ( .txt )
    Next
EndFunc   

; Function to populate the right edit.
Func View_tutor()
    $s_text = GUICtrlRead($TutorItList) ; read the selected file to a variable.
    If $s_text = "" Then Return
    $s_text = @HomeDrive & "\" & $s_text & ".txt" ; set the location of the file.
    Dim $Tut_text
    If Not _FileReadToArray($s_text, $Tut_text) Then ; read the file to an array.
        MsgBox(4096, "Tutor Error", " Error reading log to Array     error:" & @error)
        Return
    EndIf
    GUICtrlSetData($TutorItEdit, "") ; set the edit to empty.
    For $x = 1 To $Tut_text[0] ; for loop to place the read file into the edit.
        GUICtrlSetData($TutorItEdit, $Tut_text[$x] & @CRLF, 1)
    Next
EndFunc   

; Note
; file read to array, reads the file
; file list to array, lists the files

8)

NEWHeader1.png

Link to comment
Share on other sites

Lesson #6 from "Welcome to Autoit 1-2-3"

8)

Thanks for making me feel more stupid Val...

:whistle::)

I went through that tutorial like MONTHS ago, I can't remember the content of each lesson,

Maybe it's time for a refresher

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