Thursday, September 19, 2019

#1 2019-04-12 12:16:42 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 542

Cleaning duplicate files

The  following script finds and cleans all my Mac from duplicates in few seconds.

I have created Smart Folder named LargeFiles.savedSearch, which I have saved in folder Saved Searches of folder Library of my home directory. This smart folder is created with "Search Mac" and using criteria "File Size  greater than 1000 Kbyte".


Applescript:


set Protected_List to {"h"} -- protected files list (extensions)
set theSmartFolder to alias "Untitled:Users:123:Library:Saved Searches:LargeFiles.savedSearch"

tell application "Finder"
   activate
   open theSmartFolder
   tell application "System Events" to tell process "Finder"
       click menu bar item "Edit" of menu bar 1
       delay 0.5
       tell menu item "Select All" of menu 1 of menu bar item "Edit" of menu bar 1 to if (exists) then click
   end tell
   -- Getting Finder paths list for files, matching to criteria above
   set theSelection to selection
   close windows
end tell

set duplicate_Files to 0
set saved_Space to 0
set all_Files to count theSelection

tell application "Finder"
   repeat with n from 1 to all_Files - 1
       if exists (item n of theSelection) then
           set item_1 to item n of theSelection
           -- skip comparing, if item_1 is protected by filename extension's list above
           if not (name extension of item_1 is in Protected_List) then
               set type_1 to file type of item_1
               -- skip comparing, if item_1 is application
               if type_1 ≠ "APPL" then
                   set posix_Path_1 to quoted form of (POSIX path of (item_1 as alias))
                   set size_1 to size of item_1
                   
                   repeat with m from n + 1 to all_Files
                       set item_2 to item m of theSelection
                       set size_2 to size of item_2
                       -- skip byte by byte comparing if file's sizes isn't same
                       if size_1 = size_2 then
                           set type_2 to file type of item_2
                           -- byte by byte comparing ONLY if is same file's types
                           if type_1 = type_2 then
                               set posix_Path_2 to quoted form of (POSIX path of (item_2 as alias))
                               set shell_Command_String to "cmp -s" & posix_Path_1 & space & posix_Path_2
                               set exitStatus to 0 -- setting initial status of shell operation
                               try
                                   set exitStatus to do shell script shell_Command_String
                                   return exitStatus
                               end try
                               if exitStatus = 0 then -- if exitStatus remains= 0, that indicates "files is same"                    
                                   display dialog "Founded duplicate files!!!" & return & return & posix_Path_1 & return & return & posix_Path_2 & return & return & "REMOVE DUPLICATE FILE ?" buttons {"SKIP", "REMOVE 1st FILE", "REMOVE 2nd FILE"}
                                   set button_Returned to button returned of result
                                   
                                   if button_Returned is "REMOVE 2nd FILE" then
                                       set duplicate_Files to duplicate_Files + 1
                                       set locked of item_2 to false
                                       move item_2 to trash -- removing duplicate file
                                       set saved_Space to saved_Space + size_2
                                   end if
                                   if button_Returned is "REMOVE 1st FILE" then
                                       set duplicate_Files to duplicate_Files + 1
                                       set locked of item_1 to false
                                       move item_1 to trash -- removing duplicate file
                                       set saved_Space to saved_Space + size_1
                                       exit repeat
                                   end if
                               end if
                           end if
                       else
                           exit repeat
                       end if
                   end repeat
               end if
           end if
       end if
   end repeat
end tell

tell me to activate
-- Creating report message
display alert "Total files maching to criteria: " & (all_Files as string) & return & return & "Removed duplicate files: " & (duplicate_Files as string) & return & return & "Cleaned Disk Space: " & (saved_Space / 1024 as string) & " Kbytes"

Last edited by KniazidisR (2019-09-04 03:21:26 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#2 2019-04-13 07:35:02 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 542

Re: Cleaning duplicate files

Made improvements to the script above. Now you can choose which of the 2 compared files to delete, and which to leave. The speed, flexibility and practicality of the script is much better than its professional counterparts...

Last edited by KniazidisR (2019-04-13 07:48:03 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)