Jump to content

A very simple script -i hope!


happy2help
 Share

Recommended Posts

Hi All

I am hoping someone can help me get started with a GUI. All me scripts up to now have not needed a GUI (or i have found a work around)

Can someone get me started on a GUI that reads a list of au3 files in B:\scripts\To Use\ and reads the folders under B:\scripts and lists them somehow so i can click on a au3 listed and a folder listed, click go and it will copy the file from "to use" to the other folder.

I then have a script which runs on each of my machines that monitors it's own folder under B:\scripts and runs any au3 files it finds there.

I am not asking for someone to write it for me, just maybe so examples on how you would do it, so i can learn how to make scripts with GUIs built in too.

A few months ago, i looked at Koda and got totally confused where to start.

Any help would be grateful as I'm stuck using this massive part of autoit.

The script i use to run any found au3s is this

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=..\Icons\DocumentRepository.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#RequireAdmin
#include "B:\Scripts\File.au3"
Opt("WinWaitDelay", 850) ;250 milliseconds
Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
;Opt("SendKeyDelay", 50)          ;50 milliseconds
If @compiled = 0 Then HotKeySet("{ESC}", "Exit1")
Func Exit1()
    Exit
EndFunc

MsgBox(0, "WAIT", "FolderMonitor now running", 10)

While 1

    $filename = "FolderMonitor.ini"
    $file = FileOpen($filename, 0)
    $progy = "Empty"
    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.", 20)
        Sleep(300000)
        ContinueLoop
    EndIf
    
    ; Read in lines of text until the EOF is reached
    While 1
        $line = "B:\Scripts\" & @ComputerName & "\" & FileReadLine($file)
        If @error = -1 Then ExitLoop
        If FileExists ($line) Then
            RunWait(@AutoItExe & " /AutoIt3ExecuteScript " & $line)
            If $line = "B:\Scripts\" & @ComputerName & "\RestartServer.au3" Then
                $progy = $line
            EndIf
            Sleep (5000)
            FileDelete($line)
        EndIf
    Wend
    If $progy = "B:\Scripts\" & @ComputerName & "\RestartServer.au3" Then Shutdown(6)
    FileClose($file)
    Sleep(300000)
WEnd
Link to comment
Share on other sites

  • Moderators

happy2help,

How I would do what you are looking to do:

Create a GUI with 2 lists side by side. Use FileListToArray twice, once to get all the files, and then again to get all the folders. Populate the lists with the files on the left and the folders to the right. When you have selected the file and folder, have a button which reads the selections and then runs code to transfer the file to the folder.

What you want to do is not difficult - honestly! :mellow: Give it a go and come back if you have problems.

If you have never created a GUI before then I strongly suggest reading BrettF's excellent tutorial here - Section 9 deals with GUIs and will give you a good basis to work with. There is even a bit about Koda in there! :(

M23

Edit: If you look here you will get an idea of what I mean. :lol:

Edited by Melba23

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

i have got this far

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <File.au3>
#Include <Array.au3>

$FileList=_FileListToArray("B:\Scripts\To Use\", "*", 1)
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
_ArrayDisplay($FileList,"$FileList")

$FolderList=_FileListToArray("B:\Scripts\", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
_ArrayDisplay($FolderList,"$FolderList")

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Script Copier", 625, 443, 193, 125)
$List1 = GUICtrlCreateList("Scripts", 72, 48, 177, 318)
$List2 = GUICtrlCreateList("Machines", 256, 48, 169, 318)
$Button1 = GUICtrlCreateButton("Copy Now", 456, 48, 73, 49, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            MsgBox(0,"Done", "Done")
    EndSwitch
WEnd

but dont know how to put $FileList into $List1 ( or do i replace $List1 with $FileList)

also, how do i tell what the user has clicked on (the 2 arrays) when they click on "Copy now"?

thanks for your help, i just can't get my head around it at the moment.

Link to comment
Share on other sites

  • Moderators

happy2help,

You need to start reading that Help file more closely! :(

but dont know how to put $FileList into $List1

You add data to a control with GUICtrlSetData. From the Help file page for that command:

data - Combo, List, ListView, ListViewItem: An Opt("GUIDataSeparatorChar",...) separated list of items.

Now the default GUIDataSeparatorChar is "|", so we need to get the array that is returned from _FileListToArray into a single string with "|" separators - something like this:

$sFileList = ""
For $i = 1 To $aFileList[0]
    $sFileList &= $aFileList[$i] & "|"
Next

; And then add it to the list like this
GUICtrlSetData($List1, $sFileList)

Obviously you do somehting similar thing for the other list.

how do i tell what the user has clicked on

Here you use GUICtrlRead and you find the values in the two lists like this:

$Value1 = GUICtrlRead($List1)
$Value2 = GUICtrlRead($List2)

Now you have these 2 values, you can fill the parameters of the FileCopy command to be actioned whan you press the button. :mellow:

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

Thanks

I took what you said and produced this

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <File.au3>
#Include <Array.au3>

$Form1 = GUICreate("Script Copier", 625, 443, 193, 125)
$List1 = GUICtrlCreateList("", 72, 48, 177, 318)
$List2 = GUICtrlCreateList("", 256, 48, 169, 318)
$Button1 = GUICtrlCreateButton("Copy Now", 456, 48, 73, 49, 0)

$FileList=_FileListToArray("B:\Scripts\To Use\", "*", 1)
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

$sFileList = ""
For $i = 1 To $FileList[0]
    $sFileList &= $FileList[$i] & "|"
Next

GUICtrlSetData($List1, $sFileList)
$FolderList=_FileListToArray("B:\Scripts\", "*", 2)
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

$sFolderList = ""
For $i = 1 To $FolderList[0]
    $sFolderList &= $FolderList[$i] & "|"
Next

GUICtrlSetData($List2, $sFolderList)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Value1 = GUICtrlRead($List1)
            $Value2 = GUICtrlRead($List2)
            If FileExists("B:\Scripts\" & $Value2 & "\" & $Value1) Then MsgBox(0, "ERROR", "B:\Scripts\" & $Value2 & "\" & $Value1 & " already exists")
            FileCopy("B:\Scripts\To Use\" & $Value1, "B:\Scripts\" & $Value2 & "\")
            If FileExists("B:\Scripts\" & $Value2 & "\" & $Value1) Then MsgBox(0, "B:\Scripts\To Use\" & $Value1, "B:\Scripts\" & $Value2 & "\"  & $Value1 & " Exists")
    EndSwitch
WEnd

Is this right or is there a better way?

edit.. Also how do you remove a phrase from an array when you don't know where it is going to be?

I want to remove "To Use" from the array $FolderList.

I could rename the folder to "_To Use" and then remove it by using _Arraydelete ($FolderList, 1)

Edited by happy2help
Link to comment
Share on other sites

  • Moderators

happy2help,

I would remove the folder from the list like this: :mellow:

$sFolderList = ""
For $i = 1 To $aFolderList[0]
    If $aFolderList[$i] <> "To Use" Then
        $sFolderList &= $aFolderList[$i] & @CRLF
    EndIf
Next

A couple of other points:

1. I recommend you differentiate your variables a bit more clearly. Look at my example script above - arrays begin $a, strings begin $s, integers begin $i, etc. As AutoIt does not type its variables, this helps you remember just what is where - and makes the above code snippet easier to understand. :(

2. Do the listing of the files/folders BEFORE you create the GUI - then you do not risk pausing the GUI creation while doing all the array manipulation. Playing with arrays is usually timecnsuming and, particularly if you have a very large array to adjust, you could end up with an embarrassing delay while your half-formed GUI is waiting for the other code to finish.

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

Hi, thanks for your help. This is what i have come up with

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListBoxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

#Region ;reading directors and putting it in a list
$sFileList = ""
$sFileList2 = ""
$sFolderList = ""

$aFileList = _FileListToArray("B:\Scripts\To Use\", "*", 1)
If @error = 1 Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf
For $i = 1 To $aFileList[0]
    $sFileList &= $aFileList[$i] & "|"
Next
$aFolderList = _FileListToArray("B:\Scripts\", "*", 2)
If @error = 1 Then
    MsgBox(0, "", "No Files\Folders Found.")
    Exit
EndIf
For $i = 1 To $aFolderList[0]
    If $aFolderList[$i] <> "To Use" Then
        $sFolderList &= $aFolderList[$i] & "|"
    EndIf
Next
#EndRegion 

#Region ; create GUI
$Form1 = GUICreate("Script Copier", 360, 300, 193, 125)
$List1 = GUICtrlCreateList("", 20, 20, 120, 270)
$List2 = GUICtrlCreateList("", 150, 20, 120, 270)
$Button1 = GUICtrlCreateButton("Copy Now", 280, 20, 73, 49, 0)
$Button2 = GUICtrlCreateButton("Update ini File", 280, 100, 73, 49, 0)
GUICtrlSetData($List1, $sFileList)
GUICtrlSetData($List2, $sFolderList)
GUISetState(@SW_SHOW)
#EndRegion 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            CopyPressed()
        Case $Button2
            UpDate()
    EndSwitch
WEnd

Func CopyPressed()
    MsgBox(0, "Button Pressed", "About to copy the file you have selected", 3)
    $Value1 = GUICtrlRead($List1)
    $Value2 = GUICtrlRead($List2)
    If FileExists("B:\Scripts\" & $Value2 & "\" & $Value1) Then MsgBox(0, "ERROR", "B:\Scripts\" & $Value2 & "\" & $Value1 & " already exists")
    FileCopy("B:\Scripts\To Use\" & $Value1, "B:\Scripts\" & $Value2 & "\")
    If FileExists("B:\Scripts\" & $Value2 & "\" & $Value1) Then
        $answer = MsgBox(0, "B:\Scripts\To Use\" & $Value1, "B:\Scripts\" & $Value2 & "\" & $Value1 & " Exists", 30)
        If $answer = -1 Then Exit
    EndIf
EndFunc   ;==>CopyPressed

Func UpDate()
    $file = FileOpen("FolderMonitor.ini", 2)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    For $i = 1 To $aFileList[0]
        $sFileList2 &= $aFileList[$i] & @CRLF
    Next
    FileWrite($file, $sFileList2)
    FileClose($file)
    MsgBox(0, "Done", "FolderMonitor.ini now Up To Date.")
EndFunc   ;==>UpDate

Any suggestions or improvements will be gratefully received

Link to comment
Share on other sites

  • Moderators

happy2help,

Count how many times you have used "B:\Scripts\To Use\" & "B:\Scripts\" in the code. Why not set 2 variables at the start of the script and then use those variables in place of the literal strings?

That sort of thing makes code much more "portable" and also helps prevent stupid typing errors (which i make far too frequently! :mellow: )

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