Hello! I have thousands of files on an external drive that I would like to copy onto my Apple iMac drive. However, there are a lot of files that are not necessary. These files have a lot of extra characters at the end of the filename, which I have noted examples at the end as Excluded File End Strings.
I would like to copy any files that do not have those strings at the end of the filename. Important: most of such excluded files do not have a file extension, they just appear in Finder as "Document".
Next requirement: I would like to copy the folder structure from the source folder to the destination folder. If the folder doesn't exist in the destination then create it. For instance: source/test/2024, then create destination/test/2024 and put any files from that source sub folder in the destination subfolder.
Last requirement: Create an output text file of all of the files actually copied over to the destination. Include the path, the filename, the file extension, the date, and the file size Create a summary count of files by file extension (eg jpg, xls, txt, etc) Create a grand total count of all files copied.
Variables: Source file path: /Users/myfiles/Desktop/Cisco
Destination file path: /Users/myfiles/Desktop/Destination Test
Excluded File End Strings: /AFP_AfpInfo /com.apple.system.Security /com.apple.timemachine.private.directorycompletiondate /com.apple.quarantine /com.apple.macl /com.apple.lastuseddate#PS /com.apple.iwork.documentUUID#PS /com.apple.Preview.UIstate /com.intuit.turbotax /com.apple.metadata/_kMDItemUserTags /com.apple.metadata/kMDLabel
I would prefer to see this in AppleScript, so that I can manage any other strings that need to be exlcluded.
I have attached a screenshot of the source folder. Note that I only want to copy the 4 highlighted files of the 16 in this folder. The others should be excluded.
I have also attached a script that I tried to write, but am getting the following error: error "Can’t get keys of {}." number -1728 from keys of {}
Do you have any suggestions as to how I can accomplish this with AppleScript or an a better way? I do not understand what is causing the error. Thank you! Brian
set sourceFolder to "/Users/blimbert/Desktop/Cisco"
set destinationFolder to "/Users/blimbert/Desktop/Destination Test"
-- List of filename patterns to exclude
set excludedPatterns to {".AFP_AfpInfo", ".com.apple.system.Security", ".com.apple.timemachine.private.directorycompletiondate", ".com.apple.quarantine", ".com.apple.macl", ".com.apple.lastuseddate#PS", ".com.apple.iwork.document", ".com.apple.Preview.UIstate", ".com.intuit.turbotax", ".com.apple.metadata/_kMDItemUserTags", ".com.apple.metadata/kMDLabel"}
-- Initialize file count by extension
set fileCountByExtension to {}
-- Get a list of files in the source folder using System Events
tell application "System Events"
set fileList to files of folder sourceFolder
end tell
-- Initialize variables to count copied files and store file listing
set numFilesCopied to 0
set fileListing to ""
-- Iterate through each file
repeat with thisFile in fileList
set fileName to name of thisFile
set excludeFile to false
-- Check if the file name matches any of the excluded patterns
repeat with excludedPattern in excludedPatterns
if fileName contains excludedPattern then
set excludeFile to true
exit repeat
end if
end repeat
if not excludeFile then
-- Get the file extension
set fileExtension to text -4 thru -1 of fileName -- Get last four characters (extension)
-- Update file count by extension
if fileExtension is not in keys of fileCountByExtension then
set fileCountByExtension's fileExtension to 1
else
set fileCountByExtension's fileExtension to (fileCountByExtension's fileExtension) + 1
end if
-- Increment the number of copied files
set numFilesCopied to numFilesCopied + 1
end if
end repeat
-- Check if any files were processed
if numFilesCopied = 0 then
display dialog "No files were processed."
else
-- Generate summary by file extension
set summaryText to "Summary by File Extension:" & linefeed
repeat with fileExtension in keys of fileCountByExtension
set countOfExtension to fileCountByExtension's fileExtension
set summaryText to summaryText & " " & fileExtension & ": " & countOfExtension & linefeed
end repeat
-- Calculate grand total
set grandTotal to sum of items of fileCountByExtension
-- Append grand total to the summary
set summaryText to summaryText & "Grand Total: " & grandTotal
-- Display a dialog box with the summary
display dialog "Total Files Copied: " & numFilesCopied & linefeed & summaryText buttons {"OK"} default button "OK"
end if