;# modified 12/5/2017 19:02 #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=Backup.exe #AutoIt3Wrapper_Compression=4 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #Region #### AutoIt Includes #### #include #include #include #include #include #include #EndRegion #### AutoIt Includes #### #Region ### Globals #### ; set the constant values for the array elements Global Enum _ $e_MAINGUI, _ $e_BACKUPDESTINATIONLBL, _ $e_INCLUDEMORELBL, _ $e_EXCLUDEAVTYPECHK, _ $e_EDITBOX, _ $e_COMPRESSBACKUPCHK, _ $e_BACKUPBTN, _ $e_FILELISTHANDLE, _ $e_ARRAYMAX ; create a global array for the controls Global $aid_Controls[$e_ARRAYMAX] Global $bln_FlashBalloon = True #EndRegion ### Globals #### GUI_Draw() ; draw the gui ; check for interrupted copy If Backup_CheckForPending() Then Backup_CopyFiles(True) While 1 Sleep(10) WEnd Func Backup_CompressFiles() Local $as_Folders = Backup_GetFolderArray() ; get an array of the selected folders If Not IsArray($as_Folders) Then Return SetError(1) Local $h_FileList = FileOpen(CompressList_ReturnFilePath(), $FO_APPEND + $FO_CREATEPATH) If $h_FileList = -1 then Return SetError(2) Local $as_FLTAR = 0 Local $s_Source = '' Local $s_Mask = '*' ; initial mask for FLTAR search If GUICtrlRead($aid_Controls[$e_EXCLUDEAVTYPECHK]) = $GUI_CHECKED Then $s_Mask = '*|*.mp3;*.mp4;*.vob;*.mpeg|' For $i = 1 To $as_Folders[0][0] $s_Source = $as_Folders[$i][1] ; update the source $as_FLTAR = _FileListToArrayRec($s_Source, $s_Mask, $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) If @error Then ContinueLoop ; #### will need changing to delete ini section if no files found #### _FileWriteFromArray($h_FileList, $as_FLTAR, 1) If @error then MsgBox(0, 'error', @error) Next FileClose($h_FileList) EndFunc ;==>Backup_CompressFiles Func Backup_CopyFiles($bln_ResumeCopy = False) Local $as_Folders = 0 Local $s_Source = '', $s_Destination = '', $s_DestinationFile = '', $s_Folder = '' If $bln_ResumeCopy Then $as_Folders = IniReadSection(INI_ReturnFilePath(), 'Pending') _FileWriteLog(Log_ReturnFilePath(), 'Resuming Copy...') Else $as_Folders = Backup_GetFolderArray() ; get an array of the selected folders ; Write the folder list to the ini. We need to do this before the copy as if the program crashes ; not all the folders may have been written to the ini file For $i = 1 To $as_Folders[0][0] $s_Folder = $as_Folders[$i][0] ; update folder name $s_Source = $as_Folders[$i][1] ; update the source $s_Destination = GUICtrlRead($aid_Controls[$e_BACKUPDESTINATIONLBL]) & '\' & $s_Folder ; update the destination ; write the folder paths to the ini, needed if program terminates IniWrite(INI_ReturnFilePath(), _ ; inifile 'Pending', _ ; section $s_Folder, _ ; key (folder) $s_Source & '|' & _ ; value (source & '|' $s_Destination) ; destinarion) Next EndIf If Not IsArray($as_Folders) Then Return SetError(1) GUICtrlSetData($aid_Controls[$e_EDITBOX], '') ; clear the edit box Local $s_Info = '' ; string for edit box updating Local $s_Mask = '*' ; initial mask for FLTAR search Local $as_FLTAR = 0 ; array for files Local $i_CopyErrors = 0 ; error counter Local $h_CompressList = -1 ; update the mask to exclude video files If GUICtrlRead($aid_Controls[$e_EXCLUDEAVTYPECHK]) = $GUI_CHECKED Then $s_Mask = '*|*.mp3;*.mp4;*.vob;*.mpeg|' If GUICtrlRead($aid_Controls[$e_COMPRESSBACKUPCHK]) = 1 Then $h_CompressList = FileOpen(CompressList_ReturnFilePath(), $FO_APPEND + $FO_CREATEPATH) If $h_CompressList = -1 then Return SetError(2) EndIf ; loop through the selected folders For $i = 1 To $as_Folders[0][0] $i_CopyErrors = 0 ; reset the error counter $s_Folder = $as_Folders[$i][0] ; update folder name If $bln_ResumeCopy Then $s_Source = StringRegExpReplace($as_Folders[$i][1], '\|.*$', '') ; get the source directory $s_Destination = StringRegExpReplace($as_Folders[$i][1], '^.*\|', '') ; get the destination directory Else $s_Source = $as_Folders[$i][1] ; update the source $s_Destination = GUICtrlRead($aid_Controls[$e_BACKUPDESTINATIONLBL]) & '\' & $s_Folder ; update the destination EndIf _FileWriteLog(Log_ReturnFilePath(), $s_Source & ' ..............Started ') ; update the edit box GUICtrlSetData($aid_Controls[$e_EDITBOX], @CRLF & 'Backing Up ' & $s_Folder & ' Folder' & @CRLF, ' ') ; get a list of files and folders in the specified folder $as_FLTAR = _FileListToArrayRec($s_Source, $s_Mask, $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) If @error Then ContinueLoop ; #### will need changing to delete ini section if no files found #### #Region copy files and folder #### ; loop through the files\folders For $j = 1 To $as_FLTAR[0] ; replace the source path with the destination path $s_DestinationFile = StringReplace($as_FLTAR[$j], $s_Source, $s_Destination) ; check if path is a directory If _WinAPI_PathIsDirectory($as_FLTAR[$j]) Then ; check if the directory is empty If _WinAPI_PathIsDirectoryEmpty($as_FLTAR[$j]) Then ; if we're resuming a copy but the folder exists, skip it If $bln_ResumeCopy And FileExists($s_DestinationFile) Then ContinueLoop ; copy the directory If Not DirCopy($as_FLTAR[$j], _ ; source $s_DestinationFile, _ ; destination $FC_OVERWRITE) _ ; overwrite existing files Then $i_CopyErrors += 1 ; increase the copy error count EndIf Else ; path is a file ; if we're resuming a copy but the file exists skip it If $bln_ResumeCopy And FileExists($s_DestinationFile) Then ; check the file size of the file in the destination. If copy interrupted the file ; could have been partially copied If FileGetSize($as_FLTAR[$j]) = FileGetSize($s_DestinationFile) Then ContinueLoop EndIf If Not FileCopy($as_FLTAR[$j], _ ; source $s_DestinationFile, _ ; destination $FC_OVERWRITE + $FC_CREATEPATH) _ ; overwite existing and create directory structure Then $i_CopyErrors += 1 ; increase the copy error count ; write the file path to the list for compression FileWrite($h_CompressList, $s_DestinationFile & @CRLF) EndIf Next #EndRegion copy files and folder #### ; delete the copied folder entry If Not $i_CopyErrors Then IniDelete(INI_ReturnFilePath(), 'Pending', $s_Folder) $s_Info = $s_Folder & ' backup Complete' Else $s_Info = $s_Folder & ' backup failed' EndIf _FileWriteLog(Log_ReturnFilePath(), $s_Source & ' ..............Completed ') ; update the edit box GUICtrlSetData($aid_Controls[$e_EDITBOX], '', ' ') GUICtrlSetData($aid_Controls[$e_EDITBOX], '---> ' & $s_Info & @CRLF, ' ') Sleep(10) Next ; close the file if opened if $h_CompressList <> -1 then FileClose($h_CompressList) ; update the edit box GUICtrlSetData($aid_Controls[$e_EDITBOX], @CRLF, ' ') GUICtrlSetData($aid_Controls[$e_EDITBOX], '#### All Folder backup jobs completed ####' & @CRLF, ' ') EndFunc ;==>Backup_CopyFiles Func Backup_CheckForPending() ; read the pending section Local $as_IRS = IniReadSection(INI_ReturnFilePath(), 'Pending') If @error Then Return False ; pending copy operation so get user action If MsgBox(BitOR($MB_ICONQUESTION, $MB_YESNO), 'Alert', 'A previous copy seems pending or was interupted ' & @CRLF & 'Select Yes to resume copying, No to skip') = $IDYES Then Return True Else IniDelete(INI_ReturnFilePath(), 'Pending') Return False EndIf EndFunc ;==>Backup_CheckForPending Func Backup_GetFolderArray() Local $i_Folders = 4 ; set the number of folders in the array ; create a 2D array with the default folders info Local $as_Folders[$i_Folders + 1][2] = _ [[$i_Folders, ''], _ ['Desktop', Desktop_ReturnFolderPath()], _ ['Favorites', Favorites_ReturnFolderPath()], _ ['Firefox', Firefox_ReturnFolderPath()], _ ['MyDocuments', MyDocuments_ReturnFolderPath()]] ; get the extra folders from the label If GUICtrlRead($aid_Controls[$e_INCLUDEMORELBL]) Then ; extra folders to copy Local $as_MoreFolders = StringSplit(GUICtrlRead($aid_Controls[$e_INCLUDEMORELBL]), '|') ; split the folders ; calculate the new number of folders Local $i_FolderCount = $i_Folders + $as_MoreFolders[0] ; resize the array to accept the new folders ReDim $as_Folders[$i_FolderCount + 1][2] ; update the folder count $as_Folders[0][0] = $i_FolderCount ; loop through the new folder list and add the info to the array For $i = 1 To $as_MoreFolders[0] $as_Folders[$i_Folders + $i][0] = _StringTitleCase(StringRegExpReplace($as_MoreFolders[$i], "^.*\\|\..*$", "")) ; get the last folder name from the source $as_Folders[$i_Folders + $i][1] = $as_MoreFolders[$i] ; source folders Next EndIf Return $as_Folders EndFunc ;==>Backup_GetFolderArray Func Backup_Run() Backup_CopyFiles() EndFunc ;==>Backup_Run Func Folder_BrowseForDestination() Local $s_Destination = FileSelectFolder('Select backup destination', '', $FSF_CREATEBUTTON + $FSF_NEWDIALOG + $FSF_EDITCONTROL) If @error Then ; user cancelled ; disable the backup button if no destination is selected If GUICtrlRead($aid_Controls[$e_BACKUPDESTINATIONLBL]) = '' Then GUICtrlSetState($aid_Controls[$e_BACKUPBTN], $GUI_DISABLE) Return EndIf ; enable the backup button GUICtrlSetState($aid_Controls[$e_BACKUPBTN], $GUI_ENABLE) ; update the label GUICtrlSetData($aid_Controls[$e_BACKUPDESTINATIONLBL], $s_Destination) ; get the type of destination drive selected Local $s_DrvType = DriveGetType($s_Destination) ; set a default message for the edit box Local $s_Message = 'The Drive you selected is Local.' & @CRLF & _ 'Please make sure you have plenty of space before proceeding with Backup' ; change message based on drive selection If $s_DrvType = 'Removable' Then $s_Message = 'The Drive you selected is an External one.' & @CRLF & _ ' This is against AON policy to copy data to external drives,' & @CRLF & _ ' tool will not be held responsible for not adhering AON policies' & @CRLF ElseIf $s_DrvType = 'Network' Then $s_Message = 'The Drive you selected is on Network.' & @CRLF & _ 'Please make sure you have plenty of space before proceeding with Backup' EndIf ; update the edit control GUICtrlSetData($aid_Controls[$e_EDITBOX], ' ') GUICtrlSetData($aid_Controls[$e_EDITBOX], $s_Message, 1) EndFunc ;==>Folder_BrowseForDestination Func Folder_BrowseForMore() ; read the old selection from the label Local $s_OldSelection = GUICtrlRead($aid_Controls[$e_INCLUDEMORELBL]) ; get any new selections from the user Local $s_Selection = FileSelectFolder('Select folders to backup', '', $FSF_CREATEBUTTON + $FSF_NEWDIALOG + $FSF_EDITCONTROL) If @error Then Return ; combine the old and new selections $s_Selection = $s_OldSelection & '|' & $s_Selection ; trim any leading pipe symbols from the string and update the label GUICtrlSetData($aid_Controls[$e_INCLUDEMORELBL], StringRegExpReplace($s_Selection, '\A[\|]+', '')) EndFunc ;==>Folder_BrowseForMore Func GUI_Draw() Opt('GUIOnEventMode', 1) $aid_Controls[$e_MAINGUI] = GUICreate('Backup', 627, 384) GUISetOnEvent($GUI_EVENT_CLOSE, 'Program_Exit') ; set the font properties for the text Local $i_FontSize = 11 Local $i_FontWeight = 400 Local $s_FontName = 'Lucida Console' #Region #### Backup controls #### GUICtrlCreateLabel('Backup Destination:', 4, 12, 175, 19, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) $aid_Controls[$e_BACKUPDESTINATIONLBL] = GUICtrlCreateLabel('', 175, 11, 353, 21, BitOR($SS_SUNKEN, $DT_END_ELLIPSIS), $WS_EX_STATICEDGE) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetBkColor(-1, $COLOR_WHITE) GUICtrlCreateButton('Browse', 540, 10, 75, 25) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, 0, $s_FontName) GUICtrlSetOnEvent(-1, 'Folder_BrowseForDestination') #EndRegion #### Backup controls #### #Region #### Include More controls #### GUICtrlCreateLabel('Include More :', 14, 51, 130, 19) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) $aid_Controls[$e_INCLUDEMORELBL] = GUICtrlCreateLabel('', 175, 48, 353, 21, BitOR($SS_SUNKEN, $DT_END_ELLIPSIS), $WS_EX_STATICEDGE) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetBkColor(-1, $COLOR_WHITE) GUICtrlCreateButton('Browse', 540, 48, 75, 25) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetOnEvent(-1, 'Folder_BrowseForMore') #EndRegion #### Include More controls #### $aid_Controls[$e_EXCLUDEAVTYPECHK] = GUICtrlCreateCheckbox('Exclude Audio/Video file types', 16, 128, 445, 17) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetOnEvent(-1, 'ExcludeAV_Clicked') $aid_Controls[$e_EDITBOX] = GUICtrlCreateEdit('', 4, 160, 613, 185) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetColor(-1, 0x00FF00) GUICtrlSetBkColor(-1, $COLOR_BLACK) $aid_Controls[$e_BACKUPBTN] = GUICtrlCreateButton('Backup', 454, 352, 75, 25) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetOnEvent(-1, 'Backup_Run') GUICtrlCreateButton('Exit', 536, 352, 75, 25) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetOnEvent(-1, 'Program_Exit') $aid_Controls[$e_COMPRESSBACKUPCHK] = GUICtrlCreateCheckbox('Compress Backup', 16, 356, 157, 17) GUICtrlSetFont(-1, $i_FontSize, $i_FontWeight, $GUI_FONTNORMAL, $s_FontName) GUICtrlSetOnEvent(-1, 'CompressBackup_Clicked') GUICtrlSetTip(-1, 'Use to compress data') GUI_LoadOptions() ; load the control states GUISetState() ; show the main gui EndFunc ;==>GUI_Draw Func GUI_LoadOptions() ; set the ExcludeAV checkbox state GUICtrlSetState($aid_Controls[$e_EXCLUDEAVTYPECHK], Number(IniRead(INI_ReturnFilePath(), 'Options', 'ExcludeAV', $GUI_UNCHECKED))) GUICtrlSetState($aid_Controls[$e_COMPRESSBACKUPCHK], Number(IniRead(INI_ReturnFilePath(), 'Options', 'CompressBackup', $GUI_UNCHECKED))) EndFunc ;==>GUI_LoadOptions Func Program_Exit() GUIDelete() Exit EndFunc ;==>Program_Exit #Region #### Checkbox Functions #### Func ExcludeAV_Clicked() ; update the ini with the checkbox state IniWrite(INI_ReturnFilePath(), 'Options', 'ExcludeAV', GUICtrlRead($aid_Controls[$e_EXCLUDEAVTYPECHK])) EndFunc ;==>ExcludeAV_Clicked Func CompressBackup_Clicked() ; update the ini with the checkbox state IniWrite(INI_ReturnFilePath(), 'Options', 'CompressBackup', GUICtrlRead($aid_Controls[$e_COMPRESSBACKUPCHK])) EndFunc ;==>CompressBackup_Clicked #EndRegion #### Checkbox Functions #### #Region #### Return Path Functions #### Func CompressList_ReturnFilePath() Return @ScriptDir & '\CompressList.txt' EndFunc ;==>CompressList_ReturnFilePath Func Desktop_ReturnFolderPath() Return @DesktopDir EndFunc ;==>Desktop_ReturnFolderPath Func Favorites_ReturnFolderPath() Return @UserProfileDir & '\Favorites' EndFunc ;==>Favorites_ReturnFolderPath Func Firefox_ReturnFolderPath() Return @AppDataDir & '\Mozilla' EndFunc ;==>Firefox_ReturnFolderPath Func INI_ReturnFilePath() Return @ScriptDir & '\CurrentDir.ini' EndFunc ;==>INI_ReturnFilePath Func Log_ReturnFilePath() Return '.\Backup.log' EndFunc ;==>Log_ReturnFilePath Func MyDocuments_ReturnFolderPath() Return @MyDocumentsDir EndFunc ;==>MyDocuments_ReturnFolderPath #EndRegion #### Return Path Functions ####