#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=icons\copy.ico #AutoIt3Wrapper_Outfile=orphaned.exe #AutoIt3Wrapper_Compile_Both=y #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; ================================================================================================================ ; LANGUAGE OPTIONS Opt("TrayMenuMode", 1) AutoItSetOption("MustDeclareVars", 0) ; ================================================================================================================ ; INCLUDES (update as needed) #include ; _ArraySort() _ArrayBinarySearch() #include ; #include ; _FileListToArrayRec() #include ; GUI #include ; GUI #include ; GUI #include ; GUI #include ; GUI ; ================================================================================================================ ; DUPLICATE INSTANCE CHECK (may need to remove or move this around depending on where it is needed) If UBound(ProcessList(@ScriptName)) > 2 Then Exit ; ================================================================================================================ ; SCRIPT IDENTITY (this is based on the filename of the script/executable) $sAppDesc = "Locate and isolate ORPHANED backup files that no longer exist in same SOURCE location" $sAppAuthor = "Sean Shrum" $sAppVersion = "1.00" $sAppName = "Orphaned" $sAppHome = "http://www.shrum.net/sean/code/" & StringLower($sAppName) $sAppUpdates = $sAppHome & "/updates.html" $sAppSupport = $sAppHome & "/support.html" $sAppFileType = "." & StringLower($sAppName) $aAppBuild = FileGetTime(@ScriptFullPath) $sAppBuild = $aAppBuild[0] & "." & $aAppBuild[1] & "." & $aAppBuild[2] & " @ " & $aAppBuild[3] & ":" & $aAppBuild[4] & ":" & $aAppBuild[5] $sAppTitle = $sAppName & " v." & $sAppVersion $sAppSettings = "HKCU\software\shrum.net\" & $sAppName ; ================================================================================================================ ; VARIABLE DECLARATIONS / DEFAULTS Local $sBackup, $sSource, $sMove, $sOrphanage, $sMove, $sAction $sFailedDir = "' could not be found and created. Check your write permissions at this location and retry. Exiting." $sNoPath = "No path specified. Exiting." $sNonexistDir = "' does not exist. Exiting" $sBackupSelect = "BACKUP location containing potential orphans..." $sSourceSelect = "SOURCE location to search for parent file..." $sOrphanageSelect = "ORPHANAGE location to copy or move identifed orphans to..." $iMsgtimeout = 20 ; ================================================================================================================ ; TRAY ICON AND TOOLTIP SET (looks for .ico in same folder as .exe otherwise uses the build icon) ;TraySetToolTip($sAppTitle) ;TraySetIcon($sAppName & ".ico") ; ================================================================================================================ ; SPLASH SCREEN / TRAYTIP BUBBLE (*only* if no command line params are present: automated run/batch call) ;if $CmdLine[0] = 0 Then Traytip($sAppTitle,$sAppHome & @CRLF & "By " & $sAppAuthor & " :: " & $sAppEmail,5) ; ================================================================================================================ ; REGISTRY UPDATES (location, version, runcount) $iRunCount = RegRead($sAppSettings, "Executed") RegWrite($sAppSettings, "Executed", "REG_SZ", $iRunCount + 1) RegWrite($sAppSettings, "", "REG_SZ", @ScriptDir) RegWrite($sAppSettings, "Version", "REG_SZ", $sAppVersion) ; ================================================================================================================ ; PROGRAM GROUP / SHORTCUTS CREATION (fires only when Executed reg value = "" from above) If $iRunCount = "" Then DirCreate(@ProgramsCommonDir & "\shrum.net\" & $sAppName) FileCreateShortcut(@ScriptFullPath, @ProgramsCommonDir & "\shrum.net\" & $sAppName & "\" & $sAppName & ".lnk") FileCreateShortcut($sAppHome, @ProgramsCommonDir & "\shrum.net\" & $sAppName & "\Homepage.lnk") FileCreateShortcut(@ScriptFullPath, @UserProfileDir & "\application data\microsoft\Internet Explorer\Quick Launch\" & $sAppName & ".lnk") EndIf ; ================================================================================================================ ; COMMAND-LINE ARGUMENT CHECK If $CmdLine[0] <> 0 Then For $i = 1 To $CmdLine[0] $aParam = StringSplit($CmdLine[$i], "=") $sParam = StringUpper(StringLeft($aParam[1], 1)) Select Case $sParam = "B" ;ackup $sBackup = $aParam[2] Case $sParam = "M" ;ove $sMove = StringUpper(StringLeft($aParam[2], 1)) Case $sParam = "O" ;rphaned $sOrphanage = $aParam[2] Case $sParam = "S" ;ource $sSource = $aParam[2] EndSelect Next If $sMove = "" Then $sMove = "N" Else ; ================================================================================================================ ; PRIOR SETTINGS PULL FOR GUI / DEFAULTS $sBackup = RegRead($sAppSettings, "Backup") $sSource = RegRead($sAppSettings, "Source") $sOrphanage = RegRead($sAppSettings, "Orphanage") $sMove = RegRead($sAppSettings, "Move") if $sMove = "" then $sMove = "N" ; ================================================================================================================ ; INPUT FORM GENERATION / MONITOR $Form1 = GUICreate($sAppName & " v." & $sAppVersion, 389, 302, 504, -1) $Label1 = GUICtrlCreateLabel("This utility will identify BACKUP files that are no longer in the SOURCE", 16, 8, 333, 17) $Label2 = GUICtrlCreateLabel("location and copy or move orphaned files to the ORPHANAGE.", 16, 24, 333, 17) $Label3 = GUICtrlCreateLabel("This utility WILL NOT remove empty directories nor will it copy or move ", 16, 48, 338, 17) $Label7 = GUICtrlCreateLabel("HIDDEN or SYSTEM files.", 16, 64, 129, 17) $Label4 = GUICtrlCreateLabel("DISCLAIMER: Use at your own risk. Click on the links below for info.", 16, 88, 354, 17) $Label8 = GUICtrlCreateLabel("Backup:", 50, 139, 55, 17) $BackupInput = GUICtrlCreateInput($sBackup, 96, 136, 233, 21) $BackupButton = GUICtrlCreateButton("...", 336, 136, 27, 21) $Label9 = GUICtrlCreateLabel("Source:", 54, 171, 52, 17) $SourceInput = GUICtrlCreateInput($sSource, 96, 168, 233, 21) $SourceButton = GUICtrlCreateButton("...", 336, 168, 27, 21) $Label10 = GUICtrlCreateLabel("Orphanage:", 34, 204, 75, 17) $OrphanageInput = GUICtrlCreateInput($sOrphanage, 96, 200, 233, 21) $OrphanageButton = GUICtrlCreateButton("...", 336, 200, 27, 21) $MoveRadio = GUICtrlCreateRadio("MOVE orphans to orphanage", 32, 232, 153, 17) $CopyRadio = GUICtrlCreateRadio("COPY orphans to orphanage", 216, 232, 153, 17) If $sMove = "Y" then GUICtrlSetState($MoveRadio, $GUI_CHECKED) Else GUICtrlSetState($CopyRadio, $GUI_CHECKED) EndIf $WebsiteLabel = GUICtrlCreateLabel("Usage info, background, downloads here...", 16, 264, 208, 17) GUICtrlSetTip(-1, $sAppHome) GUICtrlSetColor(-1, 0x0078D7) GUICtrlSetCursor (-1, 0) $SupportLabel = GUICtrlCreateLabel("Help, feature requests, error reporting here...", 16, 280, 212, 17) GUICtrlSetTip(-1, $sAppSupport) GUICtrlSetColor(-1, 0x0078D7) GUICtrlSetCursor (-1, 0) $ContinueButton = GUICtrlCreateButton("Continue", 248, 266, 67, 25) $CancelButton = GUICtrlCreateButton("Cancel", 320, 266, 59, 25) $Group1 = GUICtrlCreateGroup("Settings (prior run values are displayed)", 16, 112, 361, 145) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CancelButton Exit case $ContinueButton $sBackup = GUICtrlRead($BackupInput) $sSource = GUICtrlRead($SourceInput) $sOrphanage = GUICtrlRead($OrphanageInput) ExitLoop case $BackupButton $sBackup = FileSelectFolder($sBackupSelect, $sBackup) GUICtrlSetData($BackupInput, $sBackup) case $SourceButton $sSource = FileSelectFolder($sSourceSelect, $sSource) GUICtrlSetData($SourceInput, $sSource) case $OrphanageButton $sOrphanage = FileSelectFolder($sOrphanageSelect, $sOrphanage) GUICtrlSetData($OrphanageInput, $sOrphanage) case $MoveRadio $sMove = "Y" case $CopyRadio $sMove = "N" case $WebsiteLabel ShellExecute($sAppHome) case $SupportLabel ShellExecute($sAppSupport) EndSwitch WEnd GUIDelete($Form1) EndIf ; ================================================================================================================ ; PARAMETER VALIDATIONS If $sBackup = "" Then $sBackup = FileSelectFolder($SBackupSelect, $sBackup) If $sBackup = "" Then MsgBox(0, $sAppTitle, $sNoPath, $iMsgtimeout) Exit EndIf EndIf If FileExists($sBackup) = 0 Then MsgBox(0, $sAppName, "BACKUP @ '" & $sBackup & $sNonexistDir, $iMsgtimeout) Exit EndIf If $sSource = "" Then $sSource = FileSelectFolder("Select SOURCE location to run file list comparison against...", $sSource) If $sSource = "" Then MsgBox(0, $sAppTitle, $sNoPath, 5) Exit EndIf EndIf If FileExists($sSource) = 0 Then MsgBox(0, $sAppName, "SOURCE @ '" & $sSource & $sNonexistDir, $iMsgtimeout) Exit EndIf If $sOrphanage = "" Then $sOrphanage = FileSelectFolder("Select ORPHANAGE location...", $sOrphanage) If $sOrphanage = "" Then MsgBox(0, $sAppTitle, $sNoPath, 5) Exit EndIf $sOrphanage = StringReplace($sOrphanage, "\\", "\") EndIf If FileExists($sOrphanage) = 0 and DirCreate($sBackup) = 0 Then MsgBox(0, $sAppName, "ORPHANAGE @ '" & $sOrphanage & $sFailedDir, $iMsgtimeout) Exit EndIf If not $sMove == "Y" AND not $sMove == "N" Then $sMove = "" If $sMove = "" Then $iDefButton = 256 If RegRead($sAppSettings, "Move") = "Y" Then $iDefButton = 0 If MsgBox(4 + $iDefButton, $sAppTitle, "Do you want to MOVE the orphaned files versus just copying them?") = 6 Then $sMove = "Y" $sAction = "move" Else $sMove = "N" $sAction = "copy" EndIf EndIf ; ================================================================================================================ ; FINAL DBL CHECK CONFIRMATION If $CmdLine[0] = 0 Then $iMsgtimeout = 0 $sConfirmation = "Please double check your settings:" & @CRLF & @CRLF & _ "+ [B]ackup = '" & $sBackup & "'" & @CRLF & _ "+ [S]ource = '" & $sSource & "'" & @CRLF & _ "+ [O]phanage = '" & $sOrphanage & "'" & @CRLF & _ "+ [M]ove = '" & $sMove & "'" & @CRLF & @CRLF & _ "Confirm by pressing [Ok] or [Cancel] to exit" If MsgBox(1, $sAppTitle, $sConfirmation, $iMsgtimeout) <> 1 Then Exit ; ================================================================================================================ ; 'LAST RUN' SETTINGS SAVE RegWrite($sAppSettings, "Backup", "REG_SZ", $sBackup) $sBackupDrive = StringLeft($sBackup, 2) $sBackupDir = StringTrimLeft($sBackup, 2) RegWrite($sAppSettings, "Source", "REG_SZ", $sSource) $sSourceDrive = StringLeft($sSource, 2) $sSourceDir = StringTrimLeft($sSource, 2) RegWrite($sAppSettings, "Orphanage", "REG_SZ", $sOrphanage) $sOrphanageDrive = StringLeft($sOrphanage, 2) $sOrphanageDir = StringTrimLeft($sOrphanage, 2) RegWrite($sAppSettings, "Move", "REG_SZ", $sMove) ; ================================================================================================================ ; MAIN PROGRAM $sMsg = "Retrieving BACKUP file list from '" & $sBackup & "'" tooltip($sMsg) Local $aBackup = _FileListToArrayRec($sBackup, "*", $FLTAR_FILES + $FLTAR_NOHIDDEN + $FLTAR_NOSYSTEM, $FLTAR_RECUR) $sMsg = "Sorting " & UBound($aBackup, 1) & " BACKUP results" tooltip($sMsg) _ArraySort($aBackup, 0, 1) $sMsg = "Retrieving SOURCE file list from '" & $sSource & "'" tooltip($sMsg) Local $aSource = _FileListToArrayRec($sSource, "*", $FLTAR_FILES + $FLTAR_NOHIDDEN + $FLTAR_NOSYSTEM, $FLTAR_RECUR) $sMsg = "Sorting " & UBound($aSource, 1) & " SOURCE results" tooltip($sMsg) _ArraySort($aSource, 0, 1) $sMsg = "Identifying orphans in '" & $sBackup & "' against '" & $sSource & "'..." tooltip($sMsg) Local $aOrphans[UBound($aBackup)] $z = 0 $iStart = 1 For $i = 1 To UBound($aBackup) - 1 $iFound = _ArrayBinarySearch($aSource, $aBackup[$i], $iStart) If $iFound = -1 Then $z += 1 $aOrphans[$z] = $aBackup[$i] Else $iStart = $iFound EndIf ;If Mod($i, 1000) = 0 Then tooltip($i & " of " & ubound($aBackup, 1) & " entries parsed. " & $z & " orphans found.") Next $aOrphans[0] = $z If $aOrphans[0] > 0 Then $sMsg = $aOrphans[0] & " orphans found; beginning " & StringUpper($sAction) & " operations. A dialog will display once the process is completed." TrayTip($sAppTitle, $sMsg, 5) If FileExists($sOrphanage) = 0 Then If DirCreate($sOrphanage) = 0 Then MsgBox(0, "Could not create '" & $sOrphanage & "' folder. Exiting", 5) Exit EndIf EndIf $iSkip = 0 For $i = 1 To $aOrphans[0] If $sMove = "N" Then If FileExists($sOrphanage & "\" & $aOrphans[$i]) Then $iSkip += 1 Else FileCopy($sBackup & "\" & $aOrphans[$i], $sOrphanage & "\" & $aOrphans[$i], 8) EndIf Else If FileExists($sOrphanage & "\" & $aOrphans[$i]) = 0 Then FileDelete($sOrphanage & "\" & $aOrphans[$i]) FileMove($sBackup & "\" & $aOrphans[$i], $sOrphanage & "\" & $aOrphans[$i], 8) Else $iSkip += 1 FileDelete($sBackup & "\" & $aOrphans[$i]) EndIf EndIf Next If MsgBox(4, $sAppName, "Orphanage populated. " & $aOrphans[0] & " orphans located (" & $iSkip & " skipped due to presence in orphanage) and " & StringUpper($sAction) & "'d to: " & @CRLF & @CRLF & $sOrphanage & @CRLF & @CRLF & "Open this location in Explorer now?", 20) = 6 Then Run(@ComSpec & " /c explorer.exe /e," & '"' & $sOrphanage & '"', "", @SW_SHOWNORMAL) EndIf Else MsgBox(0, $sAppTitle, "0 orphans found.", 10) EndIf Exit