Jump to content

Employing Functions


 Share

Recommended Posts

Good day,

Years ago I took a course in programming - beginning with DOS, then Assembler and finally C+. Of course, C+ employed the use of Functions.

Since then, my professional path changed - and I began servicing computers, all of which required a working knowledge of "cmd scripting". I soon began to see the inherent weaknesses of such scripting and decided to move  into another platform. For whatever reason, I really did not want to have to re-learn C+ all-over-again. Thus, my search lead me to AutoIt! Thus far, I have been very satisfied with the move.

However, I did notice at the outset the "regular" use of Functions - though the reasons for employing such was never explicitly mentioned. Thus, I ported-over my existing cmd scripts to AutoIt! - albeit, maintaining the same structure as with cmd scripting - that is, the non-use of Functions.

What I am curious to know, is if the way in which I employ scripts would actually require - or benefit, from the use of Functions?

Thus, I will provide those that are interested, with a a sampling of one of the "ported-over" scripts to see if what I AM doing is really what I SHOULD BE DOING?...I do hope that this makes sense?

; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
; -----------------------------------------------
Local $hGUI=GUICreate("Session_Master Main Menu", 530, 305)
GUISetFont(12, $FW_BOLD, $GUI_FONTNORMAL, "Calibri")
Local $idButton_Item1=GUICtrlCreateButton("Backup Menu", 20, 20, 150, 25)
Local $idButton_Item2=GUICtrlCreateButton("Delete Menu", 20, 50, 150, 25)
Local $idButton_Item3=GUICtrlCreateButton("Restore Menu", 20, 80, 150, 25)
Local $idButton_Item4=GUICtrlCreateButton("Workspace Menu", 20, 110, 150, 25)
; -----------------
Local $idButton_Exit=GUICtrlCreateButton("Exit", 190, 20, 150, 25)
; -----------------------------------------------
GUISetState(@SW_SHOW, $hGUI)
$Pic1=GUICtrlCreatePic("D:\Install\System_Data\Images\bg_img.jpg", 360, 20, 146, 267)
; -----------------------------------------------
Local $iPID=0
; -----------------------------------------------
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $idButton_Exit
ExitLoop
; -----------------------------------------------
Case $idButton_Item1
$iPID=ShellExecute("backup_menu.au3")
Exit
; -----------------
Case $idButton_Item2
$iPID=ShellExecute("delete_menu.au3")
Exit
; -----------------
Case $idButton_Item3
$iPID=ShellExecute("restore_menu.au3")
Exit
; -----------------
Case $idButton_Item4
$iPID=ShellExecute("workspace_menu.au3")
Exit
; -----------------
EndSwitch
WEnd
; -----------------------------------------------

A heart-felt thank you to all those that respond.

PS: If the above does not suffice, I do have many, many others.

Edited by mr-es335
Link to comment
Share on other sites

For you example, it is simple enough that I don't think adding custom functions would necessarily improve anything.

From my experience, using functions is best suited for repetitive tasks that can be fed parameters/input, dynamically executed tasks, or for just modularizing code. 

Link to comment
Share on other sites

As it is now you don't have to use any functions but I don't get it. Why do you execute external AutoIt files? In a scenario where this script will be compiled as executable it makes no sense to execute external AutoIt files since this would imply that AutoIt it's installed on every PC where do you want to use this application. From my perspective the content of these file (backup_menu.au3, delete_menu.au3, etc) should be included in main script as one or more functions. In that way the final application would be a nice standalone executable that is not dependent of anything.

When the words fail... music speaks.

Link to comment
Share on other sites

Andreik,

5 hours ago, Andreik said:

Why do you execute external AutoIt files?

...'cause that is way in which I chose to worked with cmd scripts. My primary reasoning for this is what I would refer to as "bloated code"...too much all at once I simply found very confusing...

So what of this? From this...

; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
; -----------------------------------------------
Local $_src_folder1="E:\Master_Backup\3_Integrated\Session_Master"
Local $_dest_folder2="E:\Master_Backup\Session_Master"
; -----------------
Local $_sub_folder1a="E:\Master_Backup\Session_Master\Performance\Session_Undo"
Local $_sub_folder1b="E:\Master_Backup\Session_Master\Performance\Session_Data"
; -----------------------------------------------
; -----------------------------------------------
DirCopy($_src_folder1, $_dest_folder2, 1+8) 
; -----------------
DirCreate($_sub_folder1a)
DirCreate($_sub_folder1b)
; -----------------------------------------------

...to this...

#include <AutoItConstants.au3>
#include <FileConstants.au3>

Copy_Sess_Mas()
Create_Folders()

Func Copy_Sess_Mas()
    Local $_src_folder="E:\Master_Backup\3_Integrated\Session_Master"
    Local $_dest_folder="E:\Master_Backup\Session_Master"
    DirCopy($_src_folder, $_dest_folder, 1+8) 
EndFunc

Func Create_Folders()
    Local $_sub_folder_su="E:\Master_Backup\Session_Master\Performance\Session_Undo"
    Local $_sub_folder_sd="E:\Master_Backup\Session_Master\Performance\Session_Data"
    DirCreate($_sub_folder_su)
    DirCreate($_sub_folder_sd)
EndFunc

Does the latter look "workable"?

PS: Thus far, I would tend to prefer the func-tionality!

Link to comment
Share on other sites

I'm not sure what I am looking at. What I am talking about is these ShellExecute() calls.

$iPID=ShellExecute("backup_menu.au3")
..
$iPID=ShellExecute("delete_menu.au3")
..
$iPID=ShellExecute("restore_menu.au3")
..
$iPID=ShellExecute("workspace_menu.au3")

A more appropriate design would be to call some functions that does what each of these separate scripts do. So instead of $iPID=ShellExecute("backup_menu.au3") I would expect to see a call to a function named BackupMenu() that contains whatever it's inside backup_menu.au3.

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Andreik,

I simply took the DirCopy and DirCreate routines and created their own functions for those.
• I need to clearly observe "one-to-the-other". Being "slow of brain" I need to gradually move from one methodology to another. I do hope that this makes sense?

What I AM observing is 1) that I can take a large script and 2) break that script down to more, manageable-and-identifiable sections - via the employment of functions.

Once I get a literal "handle" on what I WAS DOING...I can then proceed with what I SHOULD DO - employing your suggestions as noted above.

Thanks for this...appreciated as always...

Link to comment
Share on other sites

Good day,

I have modified one of my usual non-func-tioning scripts and ported that script over with the use of functions. And though the original script consisted of 50 lines - with the updated script containing 89 lines - the func-tioning script not only looks "cleaner" more efficient to work with.

For example, rather than have to "rem out" numerous lines [that is , during testing fazes], I can now simply "rem out" the function call. Also, being able to name the functions "as you see them" is very helpful as well. Lastly, Having the variables within the function is very handy as well.

Here is the original script...

; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
; -----------------------------------------------
Local $_src_folder1="E:\Master_Backup\3_Integrated\Session_Master"
Local $_dest_folder2="E:\Master_Backup\Session_Master"
; -----------------
Local $_sub_folder1a="E:\Master_Backup\Session_Master\Performance\Session_Undo"
Local $_sub_folder1b="E:\Master_Backup\Session_Master\Performance\Session_Data"
; -----------------
Local $_src_file1="E:\Master_Backup\Session_Master\Sets\Type_2\Session_Data\*.doc"
; -----------------
Local $_sub_folder1c="E:\Master_Backup\Session_Master\Performance"
Local $_src_file2="E:\Master_Backup\Session_Master\Sets\Emcee\*.edl"
Local $_src_file3="E:\Master_Backup\Session_Master\Sets\End_Of_Performance\*.edl"
Local $_src_file4="E:\Master_Backup\Session_Master\Sets\N_Strung_Guitar\*.edl"
Local $_src_file5="E:\Master_Backup\Session_Master\Sets\S_Strung_Guitar\*.edl"
; -----------------
Local $_src_file6="E:\Master_Backup\Session_Master\Sets\Type_1\*.edl"
Local $_src_file7="E:\Master_Backup\Session_Master\Sets\Type_2\*.edl"
Local $_sub_folder1d="E:\Master_Backup\Session_Master\Sets"
; -----------------
Local $_src_file8="E:\Master_Backup\Session_Master\Shows\*.shw"
Local $_sub_folder1e="E:\Master_Backup\Session_Master\Shows"
; -----------------------------------------------
; -----------------------------------------------
SplashTextOn("NOTICE!!", "Copy/Update Session_Master folder...", 450, 50, -1, -1)
Sleep(2000)
; -----------------------------------------------
DirCopy($_src_folder1, $_dest_folder2, 1+8) 
; -----------------
DirCreate($_sub_folder1a)
DirCreate($_sub_folder1b)
; -----------------
FileCopy($_src_file1, $_sub_folder1b, $FC_OVERWRITE)
; -----------------
FileCopy($_src_file2, $_sub_folder1c, $FC_OVERWRITE)
FileCopy($_src_file3, $_sub_folder1c, $FC_OVERWRITE)
FileCopy($_src_file4, $_sub_folder1c, $FC_OVERWRITE)
FileCopy($_src_file5, $_sub_folder1c, $FC_OVERWRITE)
FileCopy($_src_file6, $_sub_folder1c, $FC_OVERWRITE)
FileCopy($_src_file7, $_sub_folder1c, $FC_OVERWRITE)
DirRemove($_sub_folder1d, $DIR_REMOVE)
; -----------------
FileCopy($_src_file8, $_sub_folder1c, $FC_OVERWRITE)
DirRemove($_sub_folder1e, $DIR_REMOVE)
; -----------------------------------------------
SplashTextOn("NOTICE!!", "Copy/Update Session_Master folder completed...", 450, 50, -1, -1)
Sleep(2000)
; -----------------------------------------------

...and here is the updated script...

; -----------------------------------------------
#include <AutoItConstants.au3>
#include <FileConstants.au3>
; -----------------------------------------------
Call_Intro()
Copy_Sess_Mas()
Create_Sub_Folders()
Copy_Set_Edls()
Copy_Doc()
Copy_Type_Edls()
Copy_Shw_Data()
Call_Outro()
; -----------------------------------------------
Func Call_Intro()
SplashTextOn("NOTICE!!", "Copy/Update Session_Master folder...", 450, 50, -1, -1)
Sleep(2000)
EndFunc
; -----------------------------------------------
Func Copy_Sess_Mas()
    Local $_src_folder="E:\Master_Backup\3_Integrated\Session_Master"
    Local $_dest_folder="E:\Master_Backup\Session_Master"
    ; ------
    DirCopy($_src_folder, $_dest_folder, 1+8) 
EndFunc
; -----------------------------------------------
Func Create_Sub_Folders()
    Local $_sub_folder_su="E:\Master_Backup\Session_Master\Performance\Session_Undo"
    Local $_sub_folder_sd="E:\Master_Backup\Session_Master\Performance\Session_Data"
    ; ------
    DirCreate($_sub_folder_su)
    DirCreate($_sub_folder_sd)
EndFunc
; -----------------------------------------------
Func Copy_Set_Edls()
    Local $_perf_folder="E:\Master_Backup\Session_Master\Performance"
    ; ------
    Local $_emcee="E:\Master_Backup\Session_Master\Sets\Emcee\*.edl"
    Local $_eop="E:\Master_Backup\Session_Master\Sets\End_Of_Performance\*.edl"
    Local $_nsg="E:\Master_Backup\Session_Master\Sets\N_Strung_Guitar\*.edl"
    Local $_ssg="E:\Master_Backup\Session_Master\Sets\S_Strung_Guitar\*.edl"
    ; ------
    FileCopy($_emcee, $_perf_folder, $FC_OVERWRITE)
    FileCopy($_eop, $_perf_folder, $FC_OVERWRITE)
    FileCopy($_nsg, $_perf_folder, $FC_OVERWRITE)
    FileCopy($_ssg, $_perf_folder, $FC_OVERWRITE)
EndFunc
; -----------------------------------------------
Func Copy_Doc()
    Local $_doc="E:\Master_Backup\Session_Master\Sets\Type_2\Session_Data\*.doc"
    Local $_sub_folder_sd="E:\Master_Backup\Session_Master\Performance\Session_Data"
    ; ------
    FileCopy($_doc, $_sub_folder_sd, $FC_OVERWRITE)
EndFunc
; -----------------------------------------------
Func Copy_Type_Edls()
    Local $_sub_folder_sd="E:\Master_Backup\Session_Master\Performance\Session_Data"
    Local $_sets_folder="E:\Master_Backup\Session_Master\Sets"
    ; ------
    Local $_t1="E:\Master_Backup\Session_Master\Sets\Type_1\*.edl"
    Local $_t2="E:\Master_Backup\Session_Master\Sets\Type_2\*.edl"
    Local $_t3="E:\Master_Backup\Procedures\Dev_P8\Session_Master\Sets\Type_3\*.edl"
    Local $_t4="E:\Master_Backup\Procedures\Dev_P8\Session_Master\Sets\Type_4\*.edl"
    ; ------
    FileCopy($_t1, $_sub_folder_sd, $FC_OVERWRITE)
    FileCopy($_t2, $_sub_folder_sd, $FC_OVERWRITE)
    FileCopy($_t3, $_sub_folder_sd, $FC_OVERWRITE)
    FileCopy($_t4, $_sub_folder_sd, $FC_OVERWRITE)
    ; ------
    DirRemove($_sets_folder, $DIR_REMOVE)
EndFunc
; -----------------------------------------------
Func Copy_Shw_Data()
    Local $_shw="E:\Master_Backup\Session_Master\Shows\*.shw"
    Local $_perf_folder="E:\Master_Backup\Session_Master\Performance"
    Local $_sub_folder_shw="E:\Master_Backup\Session_Master\Shows"
    ; ------
    FileCopy($_shw, $_perf_folder, $FC_OVERWRITE)
    ; ------
    DirRemove($_sub_folder_shw, $DIR_REMOVE)
EndFunc
; -----------------------------------------------
Func Call_Outro()
SplashTextOn("NOTICE!!", "Copy/Update Session_Master folder completed...", 450, 50, -1, -1)
Sleep(2000)
EndFunc
; -----------------------------------------------

Once I settle on a "script editor" I should then be able to "fold-and-unfold functions as well. Very nice indeed!

PS: I do hope that other neophytes will begin to see the real advantages of employing functions...even in the most simple of scripts...indeed as the HelpFile does.

Edited by mr-es335
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...