;#RequireAdmin #include #include #include #include ;Change to script directory. This fixes searching in the wrong folder when used from a shortcut with an improperly set Working Directory. FileChangeDir(@ScriptDir) ;Enables a Progress Bar ProgressOn("Recycling Duplicates", "Getting list of folders") ;Get a list of folders to search $aFolders = _FileListToArrayRec(@WorkingDir, "*", $FLTA_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;Create variable for list of duplicate files. Global $aList[0] ;Start a timer to update the Progress Bar. Global $timer = TimerInit() ;Assign a variable to track the number of found duplicates. Global $iCount = 0 ;Searches the @ScriptDir ROOT folder for duplicates to recycle. _Search(@WorkingDir) ;Searches every folder recursively in the @ScriptDir ROOT folder for duplicates to recycle. For $i = 1 To $aFolders[0] ;If timer is greater than specified value on the right, update the Progress Bar with the current information. If TimerDiff($timer) > (1000 / 29) Then ;Update the Progress Bar ProgressSet(($i * 100) / ($aFolders[0]), "Searching " & $i & " of " & $aFolders[0] & " for duplicates." & @CRLF & "Found " & $iCount & " duplicates.", "Searching for Duplicates...") ;Start a new timer to update the Progress Bar. $timer = TimerInit() EndIf ;Change Working Directory to the folder being searched. FileChangeDir($aFolders[$i]) ;Search folder for duplicates to recycle. _Search($aFolders[$i]) Next ;This allows the progress bar to fully complete itself graphically. ProgressSet(100, "Searching " & $i & " of " & $aFolders[0] & " for duplicates." & @CRLF & "Found " & $iCount & " duplicates.", "Searching for Duplicates...") Sleep(500) If $iCount = 0 Then ;Disables Progress Bar ProgressOff() ;Inform user that no duplicates were detected. MsgBox($MB_APPLMODAL, "Recycling Duplicates", "No duplicates were found. The program will now exit.") Exit EndIf ;Get the number of duplicates so we can properly update the progress bar. _ArrayInsert($aList, 0, UBound($aList), 0) ;Restart the timer $timer = TimerInit() ;For each duplicate, recycle the file. For $j = 1 To $aList[0] ;If timer is greater than specified value on the right, update the Progress Bar with the current information. If TimerDiff($timer) > (1000 / 29) Then ;Update the Progress Bar ProgressSet(($j * 100) / ($aList[0]), "Recycling duplicate " & $j & " of " & $aList[0] & ".", "Recycling Duplicates...") ;Start a new timer to update the Progress Bar. $timer = TimerInit() EndIf ;Recycle the file ;TESTING ONLY - NO FILES ARE ACTUALLY BEING RECYCLED. ;$iRecycle = FileRecycle($aList[$j]) ;If $iRecycle = False Then MsgBox($MB_SYSTEMMODAL, "Recycling Duplicates", "An error occurred whilst recycling the file.") Next ;This allows the progress bar to fully complete itself graphically. ProgressSet(100, "Recycling duplicate " & $j & " of " & $aList[0] & ".", "Recycling Duplicates...") Sleep(500) ;Disables Progress Bar ProgressOff() ;Inform user that all detected duplicates have been moved to the Recycling Bin. This implies that duplicates can be restored from the recycling bin or permanently deleted. MsgBox($MB_APPLMODAL, "Recycling Duplicates", "All found duplicates have been moved to the Recycling Bin.") ; Open the recycle bin by using the following CLSID. ShellExecute("::{645FF040-5081-101B-9F08-00AA002F954E}") Func _Search($Dir) ; Assign a Local variable the search handle of all files in the current directory. Local $hSearch = FileFindFirstFile("*") ; Check if the search was successful. If not, returns False and search the next folder. If $hSearch = -1 Then Return False ; Assign a Local variable the empty string which will contain the files names found. Local $sFileName = "", $iResult = 0 Local $vCount=0 While 1 $sFileName = FileFindNextFile($hSearch) ; If there is no more file matching the search, exit the loop and search the next folder. If @error Then ExitLoop $vCount+=1 ConsoleWrite("Checking "&$Dir&"\"&$sFileName&" >> ") Select ;Blacklist certain duplicates Case StringInStr($sFileName, "(1)") Case StringInStr($sFileName, "(2)") Case StringInStr($sFileName, "(3)") Case StringInStr($sFileName, "(4)") Case StringInStr($sFileName, "(5)") Case StringInStr($sFileName, "(6)") Case StringInStr($sFileName, "(7)") Case StringInStr($sFileName, "(8)") Case StringInStr($sFileName, "(9)") Case StringInStr($sFileName, "(conflicted)") Case StringInStr($sFileName, "(conflicted 1)") Case StringInStr($sFileName, "(conflicted 2)") Case StringInStr($sFileName, "(conflicted 3)") Case StringInStr($sFileName, "(conflicted 4)") Case StringInStr($sFileName, "(conflicted 5)") Case StringInStr($sFileName, "(conflicted 6)") Case StringInStr($sFileName, "(conflicted 7)") Case StringInStr($sFileName, "(conflicted 8)") Case StringInStr($sFileName, "(conflicted 9)") Case StringInStr($sFileName, "-BetaLaptop") Case Else ;Everything else is ignored. ConsoleWrite("[Ignored]"&@CRLF) ExitLoop EndSelect ;Add to recycle list. ConsoleWrite("[Marked]"&@CRLF) $iret = _ArrayAdd($aList,@WorkingDir & "\" & $sFileName) If $iret = -1 Then MsgBox($MB_SYSTEMMODAL, "Recycling Duplicates", "An error occurred whilst adding the file to the list of duplicates." & @CRLF & "Error: " & @error) ;Increment counter $iCount += 1 WEnd ConsoleWrite("Checked " & $vCount & " items."&@CRLF) ; Close the search handle. FileClose($hSearch) EndFunc ;==>_Search