I want to use a backup created on a Pc to restore some files on my Mac. I had help getting a script to do this, but now I would like to get a log of what was copied and what was not. Here is the code (Thanks Mark!):
tell application "Finder"
set File_list to paragraphs of (read (choose file with prompt "Choose the Bad List Text File"))
set Backup_folder_path to (choose folder with prompt "Choose the Target Folder.")
set folder_path to (choose folder with prompt "Choose the Folder Containing the Files to Be Moved.")
set aliasList to (every file of folder folder_path whose name is not in File_list and name contains ".lbxlls")
repeat with i from 1 to number of items in aliasList
try
set this_item to item i of aliasList
duplicate this_item to Backup_folder_path
on error err
if err contains "An item with the same name already exists in the destination" then
set mdtd to modification date of this_item
set mdto to name of this_item
set bk to ((Backup_folder_path & mdto as string) as alias)
set mdtbk to modification date of bk
if mdtd > mdtbk then
duplicate this_item to Backup_folder_path with replacing
end if
end if
end try
end repeat
end tell
Now, I get the idea of how to add those files that are compared by date to the log, the problem I don’t seem to figure out is: how to add those new files copied (remember that according to this code "on error err’, those may or not may be copied). Any help would be great.
I haven’t tested this, but it should work and hopefully is along the lines of what you are looking for.
global logRef
set deskPath to path to desktop as Unicode text
set logPath to deskPath & "Copy_log.txt"
openLog(logPath)
tell application "Finder"
set File_list to paragraphs of (read (choose file with prompt "Choose the Bad List Text File"))
set Backup_folder_path to (choose folder with prompt "Choose the Target Folder.")
set folder_path to (choose folder with prompt "Choose the Folder Containing the Files to Be Moved.")
set aliasList to (every file of folder folder_path whose name is not in File_list and name contains ".lbxlls")
repeat with i from 1 to number of items in aliasList
try
set this_item to item i of aliasList
duplicate this_item to Backup_folder_path
my writeLog("new", (this_item as Unicode text))
on error err
if err contains "An item with the same name already exists in the destination" then
set mdtd to modification date of this_item
set mdto to name of this_item
set bk to ((Backup_folder_path & mdto as string) as alias)
set mdtbk to modification date of bk
if mdtd > mdtbk then
duplicate this_item to Backup_folder_path with replacing
my writeLog("repl", (this_item as Unicode text))
else
my writeLog("skip", (this_item as Unicode text))
end if
end if
end try
end repeat
end tell
close access logRef
on openLog(filepath)
try
close access file filepath
end try
set logRef to (open for access file filepath with write permission)
set eof logRef to 0
write "LOG OPENED " & ((current date) as string) & return to logRef
write "TYPE FILE" & return to logRef
write "---- ----" & return to logRef
return logRef
end openLog
on writeLog(code, theFile)
write code & tab & theFile & return to logRef
end writeLog
Change the line:
set logPath to deskPath and "Copy_log.txt"
to:
set logPath to deskPath & "Copy_log.txt"
Or it will not work.
Whoops, thanks for the catch Mark