Jump to content
Sign in to follow this  
mattfaust

listveiw help

Recommended Posts

mattfaust

I need some help, I have cobbled together a little script that scans a directory and makes a array of the subdirectories.

My goal it to present a list of the sub dirs it found and let the user select one for back-up

I am using the help example of Listview to display the array - but Im having some issues.

1- when the selectio is made, "Backup selected" btn is pressed, the msgbox has a " | " after ever directory name, where is that coming from?

2- I dont know how to dynamically create the listview items for every sub directory found - since the number changes when a subdir is created or deleted - any suggestion there?

Thanks in advance

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

;List of all sub-directories   in dir cmd -add /S for full path
Local $sSortedDir = StringStripWS(_GetDOSOutput('dir "C:\Program Files\AutoIt3" /A:-A /B /O:N'), 3)
;-----------------------
;ConsoleWrite(_GetDOSOutput('dir /?') & @CRLF) ; Dir help
ConsoleWrite($sSortedDir & @CRLF)
;-------------------

Local $ListDirs = StringSplit($sSortedDir, @CRLF, 3);Change string of sub-directories into an array of sub-directories.
;
Local $DirCount = _ArrayMaxIndex($ListDirs, 0,1) ; get the number of directories found,* NOTE* array starting at zero

;------------------------------------------------------------------------------



Example()

Func Example()
    Local  $msg

    GUICreate("listview items", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    Local $listview = GUICtrlCreateListView("Folders found ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    Local $button = GUICtrlCreateButton("Backup Selected?", 10, 170, 150, 20)
    Local $item1 = GUICtrlCreateListViewItem($ListDirs[0], $listview)
    Local $item2 = GUICtrlCreateListViewItem($ListDirs[1], $listview)
    Local $item3 = GUICtrlCreateListViewItem($ListDirs[2], $listview)
    Local $item4 = GUICtrlCreateListViewItem($ListDirs[3], $listview)
    Local $item5 = GUICtrlCreateListViewItem($ListDirs[4], $listview)
    Local $item6 = GUICtrlCreateListViewItem($ListDirs[5], $listview)
    ;Local $item7 = GUICtrlCreateListViewItem($ListDirs[6], $listview)
    ;Local $item8 = GUICtrlCreateListViewItem($ListDirs[7], $listview)
    ;Local $item9 = GUICtrlCreateListViewItem($ListDirs[8], $listview)
    ;Local $input1 = GUICtrlCreateInput("", 20, 200, 150)
    ;GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping
    GUISetState()
    ;GUICtrlSetData($item2, $ListDirs[0])
    ;GUICtrlSetData($item3, "||COL33")
    ;GUICtrlDelete($item1)

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example




Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc ;==>_GetDOSOutput

Share this post


Link to post
Share on other sites
mattfaust

Just realized that I posted to the examples forum and not support - Sorry about that, I dont see how to change it now

Share this post


Link to post
Share on other sites
Melba23

mattfaust,

Do not worry, I have reported it and it will get moved over as soon as a Mod notices. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
mattfaust

Thanks,

I have made a little progess, Im sure its not " correct" BUT it is working!

I made if then statements to decide how many items in the list to create - crude I know, but it getting the job done.

also found an option to set the default seperator

Opt("GUIDataSeparatorChar","") ;"|" is the default

that seems to get rid of the " | " I was seeing

any suggestions on a cleaner approach?

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

;List of all sub-directories   in dir cmd -add /S for full path
Local $sSortedDir = StringStripWS(_GetDOSOutput('dir "C:\Program Files\AutoIt3" /A:-A /B /O:N'), 3)
;-----------------------
;ConsoleWrite(_GetDOSOutput('dir /?') & @CRLF) ; Dir help
ConsoleWrite($sSortedDir & @CRLF)
;-------------------

Local $ListDirs = StringSplit($sSortedDir, @CRLF, 3);Change string of sub-directories into an array of sub-directories.
;
Local $DirCount = _ArrayMaxIndex($ListDirs, 0,1) ; get the number of directories found,* NOTE* array starting at zero

;------------------------------------------------------------------------------



Example()

Func Example()
    Local  $msg
    Opt("GUIDataSeparatorChar","") ;"|" is the default - get rid of the trailing "|: in the menu selections

    GUICreate("Backup Selection", 220, 250, 100, 200, -1)
    ;GUISetBkColor(0x00E0FFFF)  ; will change background color

    Local $listview = GUICtrlCreateListView($DirCount&" Folders found ", 10, 40, 200, 150)
    Local $button1 = GUICtrlCreateButton("Backup Selected?", 10, 195, 150, 20)
    Local $button2 = GUICtrlCreateButton("Backup All?", 10, 220, 150, 20)
if ($DirCount >=1 )then
    Local $item1 = GUICtrlCreateListViewItem($ListDirs[0], $listview)
Else
    MsgBox(0, "Backup", "no Folders Found!", 3)
EndIf

if ($DirCount >=2 )then
    Local $item2 = GUICtrlCreateListViewItem($ListDirs[1], $listview)
EndIf
if ($DirCount >=3 )then
    Local $item3 = GUICtrlCreateListViewItem($ListDirs[2], $listview)
Endif
if ($DirCount >=4 )then
    Local $item4 = GUICtrlCreateListViewItem($ListDirs[3], $listview)
EndIf
if ($DirCount >=5 )then
    Local $item5 = GUICtrlCreateListViewItem($ListDirs[4], $listview)
endif
if ($DirCount >=6 )then
    Local $item6 = GUICtrlCreateListViewItem($ListDirs[5], $listview)
EndIf
if ($DirCount >=7 )then
    Local $item7 = GUICtrlCreateListViewItem($ListDirs[6], $listview)
EndIf
if ($DirCount >=8 )then
    Local $item8 = GUICtrlCreateListViewItem($ListDirs[7], $listview)
endif
if ($DirCount >=9 )then
    Local $item9 = GUICtrlCreateListViewItem($ListDirs[8], $listview)
EndIf
if ($DirCount >=10 )then
    Local $item10 = GUICtrlCreateListViewItem($ListDirs[9], $listview)
EndIf
if ($DirCount >=11 )then
    Local $item11 = GUICtrlCreateListViewItem($ListDirs[10], $listview)
Endif
if ($DirCount >=12 )then
    Local $item12 = GUICtrlCreateListViewItem($ListDirs[11], $listview)
EndIf
if ($DirCount >=13 )then
    Local $item13 = GUICtrlCreateListViewItem($ListDirs[12], $listview)
endif
if ($DirCount >=14 )then
    Local $item14 = GUICtrlCreateListViewItem($ListDirs[13], $listview)
EndIf
if ($DirCount >=15 )then
    Local $item15 = GUICtrlCreateListViewItem($ListDirs[14], $listview)
EndIf
if ($DirCount >=16 )then
    Local $item16 = GUICtrlCreateListViewItem($ListDirs[15], $listview)
endif
if ($DirCount >=17 )then
    Local $item17 = GUICtrlCreateListViewItem($ListDirs[16], $listview)
EndIf

if ($DirCount >=18 )then
    Local $item18 = GUICtrlCreateListViewItem($ListDirs[17], $listview)
EndIf
if ($DirCount >=19 )then
    Local $item19 = GUICtrlCreateListViewItem($ListDirs[18], $listview)
Endif
if ($DirCount >=20 )then
    Local $item20 = GUICtrlCreateListViewItem($ListDirs[19], $listview)
EndIf
if ($DirCount >=21 )then
    Local $item21 = GUICtrlCreateListViewItem($ListDirs[20], $listview)
endif
if ($DirCount >=22 )then
    Local $item22 = GUICtrlCreateListViewItem($ListDirs[21], $listview)
EndIf
if ($DirCount >=23 )then
    Local $item23 = GUICtrlCreateListViewItem($ListDirs[22], $listview)
EndIf
if ($DirCount >=24)then
    Local $item24 = GUICtrlCreateListViewItem($ListDirs[23], $listview)
endif
if ($DirCount >=25 )then
    Local $item25 = GUICtrlCreateListViewItem($ListDirs[24], $listview)
EndIf
if ($DirCount >=26 )then
    Local $item26 = GUICtrlCreateListViewItem($ListDirs[25], $listview)
EndIf
if ($DirCount >=27 )then
    Local $item27 = GUICtrlCreateListViewItem($ListDirs[26], $listview)
Endif
if ($DirCount >=28 )then
    Local $item28 = GUICtrlCreateListViewItem($ListDirs[27], $listview)
EndIf
if ($DirCount >=29 )then
    Local $item29 = GUICtrlCreateListViewItem($ListDirs[28], $listview)
endif
if ($DirCount >=30 )then
    Local $item30 = GUICtrlCreateListViewItem($ListDirs[29], $listview)
EndIf
if ($DirCount >=31 )then
    Local $item31 = GUICtrlCreateListViewItem($ListDirs[30], $listview)
EndIf
if ($DirCount >=32 )then
    Local $item32 = GUICtrlCreateListViewItem($ListDirs[31], $listview)
endif
if ($DirCount >=33 )then
    Local $item33 = GUICtrlCreateListViewItem($ListDirs[32], $listview)
EndIf

    ;Local $input1 = GUICtrlCreateInput("", 20, 200, 150)
    ;GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping
    GUISetState()
    ;GUICtrlSetData($item2, $ListDirs[0])
    ;GUICtrlSetData($item3, "||COL33")
    ;GUICtrlDelete($item1)

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button1
                MsgBox(0, "Backup", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $button2
                MsgBox(0, "backup", " backup everything ", 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example




Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc ;==>_GetDOSOutput

Share this post


Link to post
Share on other sites
Melba23

mattfaust,

Hello again! :D

This is how I would do it (assuming that I got the sub-folders in the same way):

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $listview, $fDblClk

; In the main script all variables are Global

;List of all sub-directories   in dir cmd -add /S for full path
Global $sSortedDir = StringStripWS(_GetDOSOutput('dir "C:\Program Files\AutoIt3" /A:-A /B /O:N'), 3)

Global $ListDirs = StringSplit($sSortedDir, @CRLF, 1) ; Keep count for nextline

Global $DirCount = $ListDirs[0] ; Already in the array fron StringSplit

Example()

Func Example()
    Local $msg, $iIndex

    GUICreate("listview items", 220, 250, 100, 200) ; , -1, $WS_EX_ACCEPTFILES) ; Do you really want DRAG'N'DROP enabled?
    GUISetBkColor(0x00E0FFFF) ; will change background color

    $listview = GUICtrlCreateListView("Folders found ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    Local $button = GUICtrlCreateButton("Backup Selected?", 10, 170, 150, 20)

    GUISetState()

    ; Mark the start of the listviwitem ControlIDs
    Local $iListViewStart = GUICtrlCreateDummy()
    For $i = 1 To $DirCount
        GUICtrlCreateListViewItem($ListDirs[$i], $listview)
    Next

    ; Look for the "doubleclick" message from the listview
    GUIRegisterMsg($WM_NOTIFY, "MY_WM_NOTIFY")

    Do
        ; Looking for the button is easy
        $msg = GUIGetMsg()
        Select
            Case $msg = $button
                ; Here we subtract the ControlID of the selected item from the dummy at the start
                $iIndex = GUICtrlRead($listview) - $iListViewStart
                ; And here we get that item from the original array
                MsgBox(0, "listview item", $ListDirs[$iIndex])
        EndSelect

        ; And here we see if the listview has been doubleclicked
        If $fDblClk = True Then
            $fDblClk = False
            ; See above
            $iIndex = GUICtrlRead($listview) - $iListViewStart
            MsgBox(0, "listview item", $ListDirs[$iIndex])
        EndIf

    Until $msg = $GUI_EVENT_CLOSE

EndFunc   ;==>Example

Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc   ;==>_GetDOSOutput

; React to double clicks on  ListView
Func MY_WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    If DllStructGetData($tNMHDR, 1) = GUICtrlGetHandle($listview) Then
        If DllStructGetData($tNMHDR, 3) = $NM_DBLCLK Then $fDblClk = True
    EndIf
    $tNMHDR = 0

    Return $GUI_RUNDEFMSG

EndFunc   ;==>MY_WM_NOTIFY

I have commented the code fairly well, so please ask if anything is unclear. The GUIRegisterMsg section is explained here - again, ask if you do not understand. :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
mattfaust

Wow, I knew there was a better way - that will give me something to study for a while, hopefully I get the hang of this stuff

thanks for all your help - this is the way I tend to figure stuff out, dive in and start trying things

One last thing.

I would like to make the path that sets the parent directory to a varible that I can change on user input.

what is wrong with this?

Global $SearchDir = "C:\Program Files\AutoIt3"

; In the main script all variables are Global

;List of all sub-directories   in dir cmd -add /S for full path
Global $sSortedDir = StringStripWS(_GetDOSOutput('dir '&$SearchDir&' /A:-A /B /O:N'), 3)

(assuming that I got the sub-folders in the same way):

Am I going about it the wrong way - I struggled with that also

mattfaust,

Hello again! :D

This is how I would do it (assuming that I got the sub-folders in the same way):

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $listview, $fDblClk

; In the main script all variables are Global

;List of all sub-directories   in dir cmd -add /S for full path
Global $sSortedDir = StringStripWS(_GetDOSOutput('dir "C:\Program Files\AutoIt3" /A:-A /B /O:N'), 3)

Global $ListDirs = StringSplit($sSortedDir, @CRLF, 1) ; Keep count for nextline

Global $DirCount = $ListDirs[0] ; Already in the array fron StringSplit

Example()

Func Example()
    Local $msg, $iIndex

    GUICreate("listview items", 220, 250, 100, 200) ; , -1, $WS_EX_ACCEPTFILES) ; Do you really want DRAG'N'DROP enabled?
    GUISetBkColor(0x00E0FFFF) ; will change background color

    $listview = GUICtrlCreateListView("Folders found ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    Local $button = GUICtrlCreateButton("Backup Selected?", 10, 170, 150, 20)

    GUISetState()

    ; Mark the start of the listviwitem ControlIDs
    Local $iListViewStart = GUICtrlCreateDummy()
    For $i = 1 To $DirCount
        GUICtrlCreateListViewItem($ListDirs[$i], $listview)
    Next

    ; Look for the "doubleclick" message from the listview
    GUIRegisterMsg($WM_NOTIFY, "MY_WM_NOTIFY")

    Do
        ; Looking for the button is easy
        $msg = GUIGetMsg()
        Select
            Case $msg = $button
                ; Here we subtract the ControlID of the selected item from the dummy at the start
                $iIndex = GUICtrlRead($listview) - $iListViewStart
                ; And here we get that item from the original array
                MsgBox(0, "listview item", $ListDirs[$iIndex])
        EndSelect

        ; And here we see if the listview has been doubleclicked
        If $fDblClk = True Then
            $fDblClk = False
            ; See above
            $iIndex = GUICtrlRead($listview) - $iListViewStart
            MsgBox(0, "listview item", $ListDirs[$iIndex])
        EndIf

    Until $msg = $GUI_EVENT_CLOSE

EndFunc   ;==>Example

Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc   ;==>_GetDOSOutput

; React to double clicks on  ListView
Func MY_WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    If DllStructGetData($tNMHDR, 1) = GUICtrlGetHandle($listview) Then
        If DllStructGetData($tNMHDR, 3) = $NM_DBLCLK Then $fDblClk = True
    EndIf
    $tNMHDR = 0

    Return $GUI_RUNDEFMSG

EndFunc   ;==>MY_WM_NOTIFY

I have commented the code fairly well, so please ask if anything is unclear. The GUIRegisterMsg section is explained here - again, ask if you do not understand. :huggles:

M23

Share this post


Link to post
Share on other sites
Melba23

mattfaust,

Am I going about it the wrong way?

Look at _FileListToArray in the Help file. :D It will also help with the path setting:

Global $SearchDir = "C:\Program Files\AutoIt3"

Global $sSortedDir = _FileListToArray($SearchDir, "*", 2)

Global $DirCount = $sSortedDir[0]

You might want to look at InputBox to get the folder from the user. :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.