Jump to content

Create MessageLoop from Array


Recommended Posts

I'm trying to add a section onto my script that allows you to delete xml files before it combines all of them. This script does all of that, if I manually enter the array number in the case. How do I generate a Select loop that corresponds with the array of buttons I've generated?

#include <array.au3> 
#include <guiconstantsex.au3>
#include "GUIScrollbars_Ex.au3"


;$folder = FileSelectFolder("Find the folder including your text files to combine", "", 2 )
;If @error = 1 Then Exit
$folder = "C:\IWDB\Genres"
FileChangeDir($folder)
;Recursive search for txt (or TXT) files only
$Array = RecursiveFileSearch($folder, "(?i)\.(xml)\z")
$Array2 = $Array
$hGUI = GUICreate("Genres", 220, 500)

$iOldOpt = Opt("GUICoordMode", 2)
GUICtrlCreateLabel("Genres", 10, 10, 200, 12)


;If any files were found
If $Array[0] > 0 Then
    ;_ArrayDisplay($Array)
    
    ;Output destination
    ;$save = FileSaveDialog("Select where to save your file" , "" , "Text files (*.txt)" , 16 )
    $save = @ScriptDir & "\genres.xml"
    If @error = 1 Then Exit
    
    
    
    ;Loop through found files

     $scroll_height = 12
    For $X = 2 to $Array[0]
      
       ; $file = FileRead($Array[$X])
       ; FileWrite($output, $file)
       $String = StringSplit($Array[$X], "\")
       $String2 = StringReplace($String[$String[0]], "_", " ")
       $String3 = StringReplace($String2, ".xml", "")
       $Array2[$X]  = GUICtrlCreateCheckBox($String3, -1, 1, 200, 15)
          $scroll_height = $scroll_height + 16
       Next
      _GUIScrollbars_Generate($hGUI, 0, $scroll_height)
        GUISetState(@SW_SHOW, "Delete Genres")
      
 
While 1
   $gMsg = GUIGetMsg()
    Switch $gMsg
Case $Array2[2]
    $MSGresult = MsgBox(4, "Genre Delete", "Delete Genre?")
      Select
      Case $MSGresult = 6 ;Yes
         GUICtrlSetState($Array2[2] , $GUI_DISABLE)
         FileDelete($Array[2])
      Case $MSGresult = 7 ;No
         GUICtrlSetState($Array2[2] , $GUI_UNCHECKED)
      EndSelect
Case $Array2[3]
    $MSGresult = MsgBox(4, "Genre Delete", "Delete Genre?")
     Select
     Case $MSGresult = 6 ;Yes
         GUICtrlSetState($Array2[3] , $GUI_DISABLE)
         FileDelete($Array[2])
     Case $MSGresult = 7 ;No
         GUICtrlSetState($Array2[3] , $GUI_UNCHECKED)
     EndSelect

   EndSwitch
Wend
;start combining disabled
 $Array = RecursiveFileSearch($folder, "(?i)\.(xml)\z")
 $output = FileOpen($save, 1)
  For $X = 1 to $Array[0]
      
      ;  $file = FileRead($Array[$X])
       ; FileWrite($output, $file)
       Next
    FileClose($output)
 EndIf
 
 
 
 
 
 
 
 
 
 
 
;RFSstartdir: Path to starting folder
;RFSpattern: RegEx pattern to match i.e. "(?i)\.(mp3)" find all mp3 files - case insensitive, "(?-i)\.(mp3|txt)" find all mp3 and txt files - case sensitive
;RFSrecurse: TRUE = Recursive, FALSE = Non-recursive
;RFSdepth: Internal use only
Func RecursiveFileSearch($RFSstartDir, $RFSpattern = ".", $RFSrecurse = true, $RFSdepth = 0)
   
    ;Ensure starting folder has a trailing slash
    If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\"

    If $RFSdepth = 0 Then
        ;Get count of all files in subfolders for initial array definition
        $RFSfilecount = DirGetSize($RFSstartDir, 1)
        Global $RFSarray[$RFSfilecount[1] + 1]
    EndIf
   
    $RFSsearch = FileFindFirstFile($RFSstartDir & "*.*")
    If @error Then Return

    ;Search through all files and folders in directory
    While 1
        $RFSnext = FileFindNextFile($RFSsearch)
        If @error Then ExitLoop
       
        ;If folder, recurse
        If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then
            If $RFSrecurse Then RecursiveFileSearch($RFSstartDir & $RFSnext, $RFSpattern, $RFSrecurse, $RFSdepth + 1)
        Else
            If StringRegExp($RFSnext, $RFSpattern, 0) Then
                ;Append filename to array
                $RFSarray[$RFSarray[0] + 1] = $RFSstartDir & $RFSnext
               
                ;Increment filecount
                $RFSarray[0] += 1
            EndIf
        EndIf
    WEnd
    FileClose($RFSsearch)

    If $RFSdepth = 0 Then
        Redim $RFSarray[$RFSarray[0] + 1]
        Return $RFSarray
    EndIf
 EndFunc   ;==>RecursiveFileSearch
Edited by mud409
Link to comment
Share on other sites

  • Moderators

Mud409,

I have amended your code so i can use it on my system (and used my recursive file search UDF which I think is better ;)) but I hope you can see how I determine which checkbox has been checked: :)

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include "GUIScrollbars_Ex.au3"
#include <RecFileListToArray.au3>

$sFolder = @ScriptDir
FileChangeDir($sFolder)
;Recursive search for files
$aList = _RecFileListToArray($sFolder, "*.bak", 1, 1, 0, 0)
$aList2 = $aList

$hGUI = GUICreate("Genres", 220, 500)

$iOldOpt = Opt("GUICoordMode", 2)

GUICtrlCreateLabel("Genres", 10, 10, 200, 12)

;If any files were found
If $aList[0] > 0 Then

    ;Output destination

    $sSave = @ScriptDir & "\genres.xml"

    ;Loop through found files
    $iScroll_Height = 12
    For $i = 1 To $aList[0]
        $String = StringSplit($aList[$i], "\")
        $String2 = StringReplace($String[$String[0]], "_", " ")
        $String3 = StringReplace($String2, ".au3", "")
        $aList2[$i] = GUICtrlCreateCheckbox($String3, -1, 1, 200, 15)
        $iScroll_Height = $iScroll_Height + 16
    Next
    ; Set the bounds for the Checkbox ControlIDs - this will only work if they are the only controls created in that loop <<<<<<<<<<<<<<
    $iStart = $aList2[1]
    $iEnd = $aList2[$aList2[0]]

    _GUIScrollbars_Generate($hGUI, 0, $iScroll_Height)
    GUISetState(@SW_SHOW, "Delete Genres")

    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg

            ; You need a Case to close the GUI <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            Case $iStart To $iEnd
                For $i = 1 To $aList2[0]
                    If $aList2[$i] = $iMsg Then
                        Switch MsgBox(4, "Genre Delete", "Delete Genre?")
                            Case 6 ;Yes
                                GUICtrlSetState($aList2[$i], $GUI_DISABLE)
                                FileDelete($aList[$i]) ; Delete file
                                _ArrayDelete($aList, $i) ; Delete from array
                                $aList[0] -= 1
                                _ArrayDisplay($aList, "After deletion")

                            Case 7 ;No
                                GUICtrlSetState($aList2[$i], $GUI_UNCHECKED)
                        EndSwitch
                        ExitLoop
                    EndIf
                Next
        EndSwitch
    WEnd
    ; How do you expect to get here?  You need something to escape from the infinite loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ;start combining disabled
    ;$aList = RecursiveFileSearch($sFolder, "(?i)\.(xml)\z") ; I do not thonk this is needed - we have deleted the elements earlier <<<<<<<<<<<
    $output = FileOpen($sSave, 1)
    For $i = 1 To $aList[0]
            ;  $file = FileRead($aList[$i])
        ; FileWrite($output, $file)
    Next
    FileClose($output)

EndIf
Please ask if you have any questions. :)

 

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