8-bit4life

Help with array

6 posts in this topic

#1 ·  Posted (edited)

Hello everyone :),

I was wondering if someone can point me in the right direction. I am attempting to read the contents of a a folder into an array and then have the file paths "scrolling"  through an input box. What is the best way to continuously have the script read the contents of the folder and scroll the information? The issue at the moment is that it goes through all the files it finds once and then exits.

Thanks!

My code thus far:

#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("PlayBack", 405, 293, 302, 218)
$Button1 = GUICtrlCreateButton("&PlayBack", 184, 160, 75, 25)
$Button2 = GUICtrlCreateButton("E&xit", 264, 160, 75, 25)
$Input1 = GUICtrlCreateInput("Click PlayBack to start", 80, 104, 257, 21)
$Label1 = GUICtrlCreateLabel("File Name:", 80, 72, 75, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



Global $dir = "C:\Users\lab\Pictures\Saved Pictures"

If $dir = "" Then Exit MsgBox(16, "Error", "No folder selected!", 5)


Global $aFiles = _FileListToArray($dir, "*.*", 1)

If Not UBound($aFiles) Then Exit MsgBox(16, "Error", "No files found!", 5)

Func _PlayBack()

For $n = 1 To Ubound($aFiles)

    GUICtrlSetData($Input1,$dir & "\" & $aFiles[$n])

    Sleep(999)

Next

EndFunc


_PlayBack()


While 1
    $nMsg = GUIGetMsg()


    Switch $nMsg


        Case $GUI_EVENT_CLOSE
            Exit


        Case $Button1


            _PlayBack()


        Case $Button2

            Exit

    EndSwitch
WEnd

 

 

 

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

8-bit4life,

Welcome to the AutoIt forums.

I would do it like this:

#include <GUIConstantsEx.au3>
#include <File.au3>

Global $dir = "C:\Users\lab\Pictures\Saved Pictures"
If $dir = "" Then Exit MsgBox(16, "Error", "No folder selected!", 5)

Global $aFiles = _FileListToArray($dir, "*.*", 1)
If @error Then Exit MsgBox(16, "Error", "No files found!", 5)

$Form1 = GUICreate("PlayBack", 405, 293, 302, 218)

$Button1 = GUICtrlCreateButton("&PlayBack", 184, 160, 75, 25)
$Button2 = GUICtrlCreateButton("E&xit", 264, 160, 75, 25)
$Input1 = GUICtrlCreateInput("Click PlayBack to start", 80, 104, 257, 21)
$Label1 = GUICtrlCreateLabel("File Name:", 80, 72, 75, 17)

GUISetState(@SW_SHOW)

; Declare variables
$fPlayBack = False
$iCounter = 1
; Set timer
$nTimer = TimerInit()

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ; Set flag
            $fPlayBack = True
        Case $Button2
            Exit
    EndSwitch

    ; If playback intiated and last change was more than 1 sec ago
    If $fPlayBack And TimerDiff($nTimer) > 1000 Then
        ; Change file name
        GUICtrlSetData($Input1, $dir & "\" & $aFiles[$iCounter])
        ; Increase counter amd reset if required
        $iCounter +=1
        If $iCounter = UBound($aFiles) Then
            $iCounter = 1
        EndIf
        ; Reset timer
        $nTimer = TimerInit()
    EndIf

WEnd

Please ask if you have any questions.

M23

P.S. When you post code in future please use Code tags - see here how to do it.  Then you get a scrolling box and syntax colouring as you can see above now I have added the tags.

 

Edited by Melba23
Typo

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

Thanks a bunch Melba23 :D. That worked like a charm.

Do you think that the same approach would work on the following code? It is something i found posted on the forum and trying to add some further functionality for a project i am working on.

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-w 5 -w 3 -w 4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

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



HotKeySet("{Esc}", "_Exit")

Global $c=0, $i, $k, $l, $chk, $fn
Global $hGDI[3], $hGraphic[2], $hImage, $hBrush1, $hBrush2, $hFamily, $hFont, $hFormat, $tLayout, $aInfo, $sString
Global Const $fsize = 8, $fh = 15
Global Const $width = @DesktopWidth, $height = @DesktopHeight
Global Const $bg_color = "000000"
;~ Global $imagedir =  FileSelectFolder("Select folder with images", "", 4, @ScriptDir)

Global $imagedir = "C:\Users\lab\Pictures\Saved Pictures"

If $imagedir = "" Then Exit MsgBox(16, "Error", "No folder selected!", 5)


Global $aFiles = _FileListToArray($imagedir, "*.*", 1)
If Not UBound($aFiles) Then Exit MsgBox(16, "Error", "No files found!", 5)







Func _Transition($image, $delay = 0, $speed = 1, $sleep = 2000)

    Local $a, $d = $c, $iX, $iY
    $hImage = _GDIPlus_ImageLoadFromFile($image)
    $iX = _GDIPlus_ImageGetWidth($hImage)
    $iY = _GDIPlus_ImageGetHeight($hImage)

    $FDesktop=$height/$width
    $Fact =1
    If $iX > $width And $FDesktop > ($iY/$iX) Then
        $Fact=$width/$iX
    ElseIf $iY > $height Then
        $Fact=$height/$iY
    EndIf
    $H1 = Round(($Fact * $iY),0)
    $W1 = Round(($Fact * $iX),0)
    ;$H0 = Round(($height-$H1)/2,0)
    ;$W0 = Round(($width-$W1)/2,0)

    _GDIPlus_GraphicsDrawImageRect($hGraphic[$d], $hImage,($width - $W1)/2, ($height - $H1) / 2,$W1,$H1)
    $hBrushFont2 = _GDIPlus_BrushCreateSolid(0xFFFFFFFF) ; text color
    $hFormat = _GDIPlus_StringFormatCreate()
    $hLayout = _GDIPlus_RectFCreate(($width - $W1)/2, ($height - $H1) / 2,$W1,$H1)
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 25, 0, 3)

    _GDIPlus_GraphicsDrawStringEx($hGraphic[$d], $sString, $hFont, $hLayout, $hFormat, $hBrushFont2)

    WinSetTrans($hGDI[$d], "", 0)
    WinSetOnTop($hGDI[$d], "", 1)
    For $a = 0 To 254 Step $speed
        WinSetTrans($hGDI[$d], "", $a)
        Sleep($delay)
    Next
    WinSetTrans($hGDI[$d], "", 254)
    WinSetOnTop($hGDI[Not ($d)], "", 0)
    WinSetTrans($hGDI[Not ($d)], "", 0)
    _GDIPlus_GraphicsClear($hGraphic[Not ($d)])
    $c = 1 - $d

    _GDIPlus_ImageDispose ($hImage) ; very important to realease the pics
    Sleep($sleep)



EndFunc   ;==>_Transition


Func Fader_Init($width, $height)


    $hGDI[2] = GUICreate("", $width, $height, (@DesktopWidth - $width)/2, (@DesktopHeight - $height)/2 , $WS_POPUP)
    $hGDI[0] = GUICreate("", $width, $height, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $hGDI[2])
    $hGDI[1] = GUICreate("", $width, $height, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $hGDI[2])
    GUISetBkColor("0x" & $bg_color, $hGDI[2])
    GUISetState(@SW_SHOW, $hGDI[2])
    GUISetState(@SW_SHOW, $hGDI[0])
    GUISetState(@SW_SHOW, $hGDI[1])
    WinSetTrans($hGDI[0], "", 0)
    WinSetTrans($hGDI[1], "", 0)
    _GDIPlus_Startup()
    $hGraphic[0] = _GDIPlus_GraphicsCreateFromHWND($hGDI[0])
    $hGraphic[1] = _GDIPlus_GraphicsCreateFromHWND($hGDI[1])
    _GDIPlus_GraphicsClear($hGraphic[0], "0xFF" & $bg_color)
    _GDIPlus_GraphicsClear($hGraphic[1], "0xFF" & $bg_color)
    $hBrush1 = _GDIPlus_BrushCreateSolid(0xFFF0F080)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, $fsize)
    $tLayout = _GDIPlus_RectFCreate(0, $height - $fh, 0, 0)


EndFunc   ;==>Fader_Init


Func _Exit()
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hGraphic[0])
    _GDIPlus_GraphicsDispose($hGraphic[1])
    GUIDelete($hGDI[0])
    GUIDelete($hGDI[1])
    GUIDelete($hGDI[2])
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Exit




$l = 1
Do
    $chk = StringRegExp($aFiles[$l], "(.*\.jpg|.*\.png|.*\.bmp|.*\.gif|.*\.JPG|.*\.PNG|.*\.BMP|.*\.GIF)", 3)
    If @error Then
        _ArrayDelete($aFiles, $l)
        $l -= 1
    EndIf
    $l += 1
Until $l = UBound($aFiles)


If UBound($aFiles) = 1 Then Exit MsgBox(16, "Error", "No images found!", 5)

Func _Main()

Fader_Init($width, $height)

For $k = 1 To UBound($aFiles)


    _Transition($imagedir & "\" & $aFiles[$k])



;~ MsgBox(0, "", $imagedir & "\" & $aFiles[$k])


Next
;~ Sleep(3000)
;~ _Exit()


EndFunc


_Main()

 

Thanks again!

Share this post


Link to post
Share on other sites

8-bit4life,

What exactly are you trying to do? Personally, I always find it easier to know the end result rather then wading through a chunk of code to try and understand what it is supposed to do.

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

Sorry about that Melba23. The end result i am going for is that of a custom screensaver. The goal is to be able to specify a folder in a central location that contains a set of images. The application would then create an array with the file names and display them using the set transitions/delays, etc. The advantage of that is that the content can be updated behind the scenes without the need for updating the end client . The application will be triggered when the computer is idle via a scheduled task or something along those lines. I hope this makes sense. Thank you again for your time.

Share this post


Link to post
Share on other sites

8-bit4life,

I see no reason why the same approach should not work with this code. But I am far from a GDI expert and I cannot confirm that the GDI sections of the script are correct. I suggest giving it a try and if you have problems starting a new thread with GDI in the title to attract the GDI gurus!

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