Jump to content

Help on Cleaning up Dirmove Scipt


Recommended Posts

Hello everyone, I have spent today putting together an Autoit script to replace the usage of several bat files, which was originally supposed to end up being less work overall, but turned into quite a project to get working, as this is the first more advanced script I have created. I used the example Simple GUI scipt as a base.

The purpose of this script is to have a simple UI to allow me to check and rename folders based on which folders exist on run. If one of the conditions is met, it renames the working folder's as whichever didn't exist on run, or if all of the 3 backup folders existed, it ends the check and creates the UI. As you can see I declare a lot of things for less typing later. I am just wondering if there is anything I can change to cut down on the amount of text.

Thank you for any help anyone gives.

; Renames Folders

#include <Constants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $g_idExit, $bNormal, $bBad, $bOnline
Global $Game = ("Directory1")   ;Game Folder
Global $Saves = ("Directory2")  ;Saves Folder
Global $Settings = ("Directory3")   ;Settings Folder
Global $Running = ("\Folder1")                      ;Ready to use Folder
Global $RB = ("\Folder2")                           ;Regular Backup
Global $NB = ("\Folder3")                           ;Bad Backup
Global $OB = ("\Folder4")                           ;Online Backup

Global $RunE = FileExists($Game & $Running) AND FileExists($Saves & $Running) AND FileExists($Settings & $Running)  ;Check for ready to use Folders
Global $RegE = FileExists($Game & $RB) AND FileExists($Saves & $RB) AND FileExists($Settings & $RB)             ;Check for Regular Backup Folders
Global $BadE = FileExists($Game & $NB) AND FileExists($Saves & $NB) AND FileExists($Settings & $NB)             ;Check for Bad Backup Folders
Global $OnlineE = FileExists($Game & $OB) AND FileExists($Saves & $OB) AND FileExists($Settings & $OB)              ;Check for Online Backup Folders

_Main()

Func _Main()
    ;Local $bNormal, $bBad, $bOnline

    GUICreate("Folder Rename", 220, 90)
    
    GUICtrlCreateLabel("Select which one to Activate.", 10, 10)
    $bNormal = GUICtrlCreateButton("Normal", 10, 50, 50, 20)
    GUICtrlSetOnEvent($bNormal, "OnNormal")
    $bBad = GUICtrlCreateButton("Bad", 60, 50, 50, 20)
    GUICtrlSetOnEvent($bBad, "OnBad")
    $bOnline = GUICtrlCreateButton("Online", 110, 50, 50, 20)
    GUICtrlSetONEvent($bOnline, "OnOnline")
    $g_idExit = GUICtrlCreateButton("Exit", 160, 50, 50, 20)
    GUICtrlSetOnEvent($g_idExit, "OnExit")

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState() ; display the GUI
    GUICtrlSetState($g_idExit, $GUI_DISABLE)
    
    Select
        Case $RunE = 1 AND $RegE = 1 AND $BadE = 1 AND $OnlineE = 1                 ;Check for folders
                MSGBOX($MB_SYSTEMMODAL, "", "All folders exist, SOMETHING IS WRONG")            ;Error if All Exist
                exit
        Case $RunE = 1 AND $RegE = 0 AND $BadE = 1 AND $OnlineE = 1                 ;Check for All but Regular Backups
                MSGBOX($MB_SYSTEMMODAL, "", "Regular Backed up.")   ;Running exists, Regular backup doesn't
                DirMove($Game & $Running, $Game & $RB)                                      ;Backs up Regular Folder in Games
                DirMove($Saves & $Running, $Saves & $RB)                                    ;Backs up Regular Folder in Saves
                DirMove($Settings & $Running, $Settings & $RB)                              ;Backs up Regular folder in Settings
                
        Case $RunE = 1 AND $RegE = 1 AND $BadE = 0 AND $OnlineE = 1                 ;Check for All but Bad Backups
                MSGBOX($MB_SYSTEMMODAL, "", "Bad Backed up.")       ;Running exists, Bad backup doesn't
                DirMove($Game & $Running, $Game & $NB)                                      ;Backs up Bad Folder in Games
                DirMove($Saves & $Running, $Saves & $NB)                                    ;Backs up Bad Folder in Saves
                DirMove($Settings & $Running, $Settings & $NB)                              ;Backs up Bad folder in Settings
                
        Case $RunE = 1 AND $RegE = 1 AND $BadE = 1 AND $OnlineE = 0                 ;Check for All but Online Backups
                MSGBOX($MB_SYSTEMMODAL, "", "Online Backed up.")        ;Running exists, Online backup doesn't
                DirMove($Game & $Running, $Game & $OB)                                      ;Backs up Online Folder in Games
                DirMove($Saves & $Running, $Saves & $OB)                                    ;Backs up Online Folder in Saves
                DirMove($Settings & $Running, $Settings & $OB)                              ;Backs up Online folder in Settings
    EndSelect

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_Main

; --------------- Functions ---------------
Func OnNormal()

            DirMove($Game & $RB, $Game & $Running)                                      ;Moves Regular backup to running in Games
            DirMove($Saves & $RB, $Saves & $Running)                                    ;Moves Regular backup to running in Saves
            DirMove($Settings & $RB, $Settings & $Running)                              ;Moves Regular backup to running in Settings

            GUICtrlSetState($bNormal, $GUI_Disable)
            GUICtrlSetState($bBad, $GUI_Enable)
            GUICtrlSetState($bOnline, $GUI_Enable)
            GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnNormal

Func OnBad()

            DirMove($Game & $NB, $Game & $Running)                                      ;Moves Bad backup to running in Games
            DirMove($Saves & $NB, $Saves & $Running)                                    ;Moves Bad backup to running in Saves
            DirMove($Settings & $NB, $Settings & $Running)                              ;Moves Bad backup to running in Settings

            GUICtrlSetState($bNormal, $GUI_Enable)
            GUICtrlSetState($bBad, $GUI_Disable)
            GUICtrlSetState($bOnline, $GUI_Enable)
            GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnBad

Func OnOnline()

            DirMove($Game & $OB, $Game & $Running)                                      ;Moves Online backup to running in Games
            DirMove($Saves & $OB, $Saves & $Running)                                    ;Moves Online backup to running in Saves
            DirMove($Settings & $OB, $Settings & $Running)                              ;Moves Online backup to running in Settings

            GUICtrlSetState($bNormal, $GUI_Enable)
            GUICtrlSetState($bBad, $GUI_Enable)
            GUICtrlSetState($bOnline, $GUI_Disable)
            GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnOnline

Func OnExit()
    
    If FileExists($Game & $Running) = 1 AND FileExists($Saves & $Running) = 1 AND FileExists($Settings & $Running) = 1 then
    exit
    Else
        MsgBox($MB_SYSTEMMODAL, "", "No Folder Selected")
    EndIf

EndFunc   ;==>OnExit

 

Edited by Yukoshoo
Link to comment
Share on other sites

This will be the only time I bump this to bring it to the top, but I am still looking for help on possibly simplifying the code or changing how the checks work, really anything that could be changed as better coding practices.

Link to comment
Share on other sites

The bulk of your globals could be locals.

The big V seemed to abhor globals unless absolutely necessary.

If memory serves correctly; ± "Too lazy to figure out how to avoid globals". It was a long & inspired speech.

The ramifications for a small script are probably small, but you did ask about better coding practices.

 

Link to comment
Share on other sites

No need for If FileExists() = 1 Then, If FileExists() Then is okay. Also use Tidy on your script, found in the full version of SciTE for AutoIt, as there is a lack of consistency amongst functions and keywords, with regards to case.

If a variable has to be global, prefix it $g_, as that will make your life easier working out what is a local variable and what is a global variable. Then there is perhaps the option of using parameters in your functions, instead of relying on global variables. I guess what I am saying is make your code more modular. In that a piece of functionality can be easily ripped out of your code and reused elsewhere, straight out of the box.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Thank you for the replies. I did suspect that I really didn't need to set all of the variables as globals, but I was getting the desired result from the script at the time. I have ran Tidy on the script, which I didn't know about as I had been using NP++, and am now using SciTE for further editing.

Then there is perhaps the option of using parameters in your functions, instead of relying on global variables. I guess what I am saying is make your code more modular. In that a piece of functionality can be easily ripped out of your code and reused elsewhere, straight out of the box.

If I understand you correctly, then my answer to this is that I don't plan to ever want a part of this to be pulled out. It is written in a way that if I wanted to, with a little bit of changing in a few places, I could easily add additional folders. If I really did want to take a part of it out, I would be referencing this script. I do understand why I wouldn't want to do this, but currently that is where I am.

I have removed the unneeded values for FileExists(). Have changed some variable names.

The initial backup on start is now its own function that runs before the GUI is created, with the variables that were only called for in it changed to local and moved inside the function.

When I get more time I need to work on being able to change which folder group I want to use without having to restart the script, and without running the backup function with a separate button.

Would just like to point out, most of the MsgBox() in this are just for debugging, to make sure things are happening when they should.

; Renames Folders for multiple Copies

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 6

Opt("GUIOnEventMode", 1)

Global $g_idExit, $g_Normal, $g_Bad, $g_Online
Global $Game = ("Directory1") ;Game Folder
Global $Saves = ("Directory2") ;Saves Folder
Global $Settings = ("Directory3") ;Settings Folder
Global $Running = ("\Folder1") ;Ready to use Folder
Global $RB = ("\Folder2") ;Regular Backup
Global $NB = ("\Folder3") ;Bad Backup
Global $OB = ("\Folder4") ;Online Backup

_Backup()
_GuiCreate()

Func _Backup()
    Local $RunExists = FileExists($Game & $Running) And FileExists($Saves & $Running) And FileExists($Settings & $Running) ;Check for ready to use Folders
    Local $RegExists = FileExists($Game & $RB) And FileExists($Saves & $RB) And FileExists($Settings & $RB) ;Check for Regular Backup Folders
    Local $BadExists = FileExists($Game & $NB) And FileExists($Saves & $NB) And FileExists($Settings & $NB) ;Check for Bad Backup Folders
    Local $OnlineExists = FileExists($Game & $OB) And FileExists($Saves & $OB) And FileExists($Settings & $OB) ;Check for Online Backup Folders

    Select
        Case $RunExists And $RegExists And $BadExists And $OnlineExists;Check for folders
            MsgBox($MB_SYSTEMMODAL, "", "All folders exist, SOMETHING IS WRONG") ;Error if All Exist
            Exit
        Case $RunExists And $RegExists = 0 And $BadExists And $OnlineExists ;Check for All but Regular Backups
            MsgBox($MB_SYSTEMMODAL, "", "Regular Backed up.") ;Running exists, Regular backup doesn't
            DirMove($Game & $Running, $Game & $RB) ;Backs up Regular Folder in Games
            DirMove($Saves & $Running, $Saves & $RB) ;Backs up Regular Folder in Saves
            DirMove($Settings & $Running, $Settings & $RB) ;Backs up Regular folder in Settings

        Case $RunExists And $RegExists And $BadExists = 0 And $OnlineExists;Check for All but Bad Backups
            MsgBox($MB_SYSTEMMODAL, "", "Bad Backed up.") ;Running exists, Bad backup doesn't
            DirMove($Game & $Running, $Game & $NB) ;Backs up Bad Folder in Games
            DirMove($Saves & $Running, $Saves & $NB) ;Backs up Bad Folder in Saves
            DirMove($Settings & $Running, $Settings & $NB) ;Backs up Bad folder in Settings

        Case $RunExists And $RegExists And $BadExists And $OnlineExists = 0 ;Check for All but Online Backups
            MsgBox($MB_SYSTEMMODAL, "", "Online Backed up.") ;Running exists, Online backup doesn't
            DirMove($Game & $Running, $Game & $OB) ;Backs up Online Folder in Games
            DirMove($Saves & $Running, $Saves & $OB) ;Backs up Online Folder in Saves
            DirMove($Settings & $Running, $Settings & $OB) ;Backs up Online folder in Settings
    EndSelect

EndFunc   ;==>_Backup

Func _GuiCreate()


    GUICreate("Folder Rename", 220, 90)

    GUICtrlCreateLabel("Select which one to play.", 10, 10)
    $g_Normal = GUICtrlCreateButton("Normal", 10, 50, 50, 20)
    GUICtrlSetOnEvent($g_Normal, "OnNormal")
    $g_Bad = GUICtrlCreateButton("Bad", 60, 50, 50, 20)
    GUICtrlSetOnEvent($g_Bad, "OnBad")
    $g_Online = GUICtrlCreateButton("Online", 110, 50, 50, 20)
    GUICtrlSetOnEvent($g_Online, "OnOnline")
    $g_idExit = GUICtrlCreateButton("Exit", 160, 50, 50, 20)
    GUICtrlSetOnEvent($g_idExit, "OnExit")

    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState() ; display the GUI
    GUICtrlSetState($g_idExit, $GUI_DISABLE)

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_GuiCreate

; --------------- Button Functions ---------------
Func OnNormal()

    DirMove($Game & $RB, $Game & $Running) ;Moves Regular backup to running in Games
    DirMove($Saves & $RB, $Saves & $Running) ;Moves Regular backup to running in Saves
    DirMove($Settings & $RB, $Settings & $Running) ;Moves Regular backup to running in Settings

    GUICtrlSetState($g_Normal, $GUI_Disable)
    GUICtrlSetState($g_Bad, $GUI_Enable)
    GUICtrlSetState($g_Online, $GUI_Enable)
    GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnNormal

Func OnBad()

    DirMove($Game & $NB, $Game & $Running) ;Moves Bad backup to running in Games
    DirMove($Saves & $NB, $Saves & $Running) ;Moves Bad backup to running in Saves
    DirMove($Settings & $NB, $Settings & $Running) ;Moves Bad backup to running in Settings

    GUICtrlSetState($g_Normal, $GUI_Enable)
    GUICtrlSetState($g_Bad, $GUI_Disable)
    GUICtrlSetState($g_Online, $GUI_Enable)
    GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnBad

Func OnOnline()

    DirMove($Game & $OB, $Game & $Running) ;Moves Online backup to running in Games
    DirMove($Saves & $OB, $Saves & $Running) ;Moves Online backup to running in Saves
    DirMove($Settings & $OB, $Settings & $Running) ;Moves Online backup to running in Settings

    GUICtrlSetState($g_Normal, $GUI_Enable)
    GUICtrlSetState($g_Bad, $GUI_Enable)
    GUICtrlSetState($g_Online, $GUI_Disable)
    GUICtrlSetState($g_idExit, $GUI_Enable)

EndFunc   ;==>OnOnline

Func OnExit()

    If FileExists($Game & $Running) And FileExists($Saves & $Running) And FileExists($Settings & $Running) Then
        Exit
    Else
        MsgBox($MB_SYSTEMMODAL, "", "No Copy Selected")
    EndIf

EndFunc   ;==>OnExit

 

Edited by Yukoshoo
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...