I am trying a alpha program for the Mac (logos). You can buy libronix resources and use them in this program for Bible research. The problem is that the alpha version is having certain issues with some of this resources. Somebody has a batch program that creates and restores backups in Windows. I want to use this backup and use it on Mac. My question is:
Is there a way to script something that would prevent those files (I already created a txt file with the file names of the problem resources) from being copied from the backup to the Mac?
I am thinking something like this : (pseudo code)
for every file
if file is on bad_list or is older than file on mac then
skip
else
copy
end
I will appreciate any help (My first Post :),
Victor
Can you show the format of the text file, for someone to pickup on this without guessing.
But reading a text file and using its contents in normally easy.
set File_list to paragraphs of (read file "Macintosh HD:Users:username:Desktop:fileList.txt")
set Backup_folder_path to "Macintosh HD:Users:username:Desktop:bkf"
set folder_path to "Macintosh HD:Users:username:Desktop:origFolder"
tell application "Finder"
set aliasList to (every file of folder folder_path whose name is not in File_list)
duplicate items of aliasList to Backup_folder_path
end tell
Here is an untested script to do the whole job – not elegant, but functional, I think:
set tPath to choose file with prompt "Choose the Bad List Text File"
set tBadList to paragraphs of (read tPath)
tell application "Finder"
set newFiles to files of (choose folder with prompt "Choose the Folder Containing the Files to Be Moved.") as alias list
set Destination to (choose folder with prompt "Choose the Target Folder.") as alias
set DestFiles to files of Destination as alias list
end tell
-- Need file names and dates
set oldFileNames to {}
set oldFileDates to {}
repeat with aFile in DestFiles
tell (info for aFile)
set end of oldFileNames to name
set end of oldFileDates to modification date
end tell
end repeat
-- Get names and dates for new files
set newFileNames to {}
set newFileDates to {}
repeat with tFile in newFiles
tell (info for tFile)
set end of newFileNames to name
set end of newFileDates to modification date
end tell
end repeat
-- check for already there, if so check for date, then move
repeat with k from 1 to count newFiles
if item k of newFileNames is not in oldFileNames then
tell application "Finder" to duplicate (item k of newFiles) to Destination
else -- there is a file by the same name, get it's location in Destination
set idx to findIt(item k of newFileNames, oldFileNames)
-- get its mod date to compare with the new file
if item k of newFileDates > item idx of oldFileDates then
tell application "Finder"
delete item idx of DestFiles
duplicate (item k of newFiles) to Destination
end tell
end if
end if
end repeat
to findIt(aName, aList) -- a simple linear search assuming not many files
repeat with k from 1 to count aList
if item k of aList is aName then
set idx to k
exit repeat
end if
end repeat
return idx
end findIt
Thank you very much, I was able to do it with the examples provided. My question is now on Adam’s post.
I understood most of you code, but I got lost somewhere near the end. I also realized that you forgot the badlist; but by the example previously provided, I figure it out.
If possible, please explain this to me.
set idx to findIt(item k of newFileNames, oldFileNames)
-- get its mod date to compare with the new file
if item k of newFileDates > item idx of oldFileDates then
tell application "Finder"
delete item idx of DestFiles
duplicate (item k of newFiles) to Destination
end tell
end if
end if
end repeat
to findIt(aName, aList) -- a simple linear search assuming not many files
repeat with k from 1 to count aList
if item k of aList is aName then
set idx to k
exit repeat
end if
end repeat
return idx
I am still not sure how this forum works, I have another question related to this. Should I start another post, or just ask here?
Here is my next question.
tell application "Finder"
set newFiles to files of (choose folder with prompt "Choose the Folder Containing the Files to Be Moved.") as alias list
set Destination to (choose folder with prompt "Choose the Target Folder.") as alias
set DestFiles to files of Destination as alias list
end tell
Is there a way to limit the files in “DestFiles” to a certain file type (*.lbxlls)?
I think I figure out my last question looking at the newest post :).
This is a modification of you code. All of this has not been tested (is just based in my observations). I replaced “new” with “backup” for clarity. The rest of the code will be the same. Now, my questions are:
Why is my code not indented (I am using spaces!)
Is this the best way to do it? and the most important question
Will this work?
set tPath to choose file with prompt "Choose the Bad List Text File"
set tBadList to paragraphs of (read tPath)
tell application "Finder"
set backup to (choose folder with prompt "Choose the Folder Containing the Files to Be Moved.") as alias
set backupFiles to every file of backup whose name is not in tBadList as alias list
set destination to (choose folder with prompt "Choose the Target Folder.") as alias
set destFiles to files of destination as alias list
end tell
-- Need file names and dates
set oldFileNames to {}
set oldFileDates to {}
repeat with aFile in destFiles
tell (info for aFile)
set end of oldFileNames to name
set end of oldFileDates to modification date
end tell
end repeat
-- Get names and dates for new files
set backupFileNames to {}
set backupFileDates to {}
set cleanBackupFiles to {}
repeat with backupFile in backupFiles
tell (info for backupFile)
if name extension = "lbxlls" then
set end of backupFileNames to name
set end of backupFileDates to modification date
set end of cleanBackupFiles to backupFile
end if
end tell
end repeat
-- check for already there, if so check for date, then move
repeat with k from 1 to count cleanBackupFiles
if item k of backupFileNames is not in oldFileNames then
tell application "Finder" to duplicate (item k of cleanBackupFiles) to destination
else -- there is a file by the same name, get it's location in Destination
set idx to findIt(item k of backupFileNames, oldFileNames)
-- get its mod date to compare with the new file
if item k of backupFileDates > item idx of oldFileDates then
tell application "Finder"
delete item idx of destFiles
duplicate (item k of cleanBackupFiles) to destination
end tell
end if
end if
end repeat
to findIt(aName, aList) -- a simple linear search assuming not many files
repeat with k from 1 to count aList
if item k of aList is aName then
set idx to k
exit repeat
end if
end repeat
return idx
end findIt
It wasn’t indented because it didn’t compile. I changed an “it” to an “in” compiled it and replaced your version above.
set backupFiles to every file of backup whose name is not in tBadList as alias list
AFAIK, this is the part not likely to work (I didn’t try) because “whose” filters don’t work in lists. I think you’ll have to set up a repeat loop of the type “repeat with k from 1 to count backupFiles” to run through the unfiltered backup file list testing each: “is in tBadList” and replacing item k of the backupFiles list with “missing value” if the item is in the bad list. Then later, you’ll have to test for “missing value” (or some other marker) before executing loop operations on the items of backupFiles, like finding names or modification dates. One way to do this is with a try block, since missing value will fail in an (info for …) statement.
I don’t have time to unravel that further, alas. Hope that helps.
It does work, but will not in yours I think because you have made it an Alias list…, where as mine is an Applescript list. document file “foo.txt” of folder “tests” of folder “Desktop” of folder “username” of folder “Users” of startup disk}
set backupFiles to every file of backup whose name is not in tBadList
*edit, Actually belay that. It returns the correct file even as an alias list.
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
I did name contains ".lbxlls", because my system would not recognise the name extension “lbxlls”
Maybe yours will?
The script only duplicates files with “.lbxlls” in the file name and whose Mod date is newer than the backup.
You should note that any script like this that uses repeats like how they are used here to process and duplicate each file rather than all file at once (like my original script) can take longer to do the job.