Duplicate files from a folder matching data from a CSV file AND...

Hello, folks.

Newbie scripter here. Please be patient with me. :slight_smile:

I am tasked with writing an Applescript which can do the following things:

  1. Ask user to point to a previously-built CSV file containing only exact filenames of desired files.
  2. Ask user to define a folder in which said named files reside. (ā€œSource Folderā€)
  3. Ask user to define a desired folder into which said files will be duplicated. (ā€œTarget Folderā€)
  4. Duplicate all files in the CSV list to the Target Folder, even if those files might be in a subfolder of the Source Folder.

Number 4 is key here, as the user is NOT expected to know where the files actually reside. They should be able to point to an entire year’s worth of job folders containing a myriad of subfolders and still be able to pull their list.

THE THING IS…

I have a script all written that can do everything EXCEPT #4, which totally kills the functionality of the script.

As written, the script fails when I point to the parent folder of the folder where my sample files live.

I get the error:

Can’t make item 5 of alias ā€œORC Studio:Job Work:ORC FTP workfiles:ORC Studio GPS:ā€ into type alias.

When the list of items is returned, I can see that the first item is an InDesign document, followed by three folders (items 2,3 & 4). Item 5 is a Photoshop file, and is the first on the list AFTER the script found some folders.

The line which reports the error is this one:
set file_source_OneNameAlias to (item i of Source_Folder) as alias

Here is the script.

Can anyone assist?





--Sur la Table script for copying files to a new location using a CSV file as source.

--This script assumes the following:
--  The CSV file contains no empty cells and no erroneous filenames
--  The files indicated in the CSV file are actually located in the SOURCE folder


tell application "Finder"
	activate
	
	--CODE for getting the INFO from a file
	set CSV_source_File to choose file with prompt "Select the CSV file containing the filenames you need to find and duplicate." without invisibles
	set OldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {return} --this MUST be return when using CSV files
	
	--CODE telling the script to make a list from the selected file.
	set Source_List to every text item of ((read CSV_source_File) as string)
	set AppleScript's text item delimiters to OldDelims
	
	--CODE for choosing your SOURCE:
	set Source_Folder to choose folder with prompt "Choose SOURCE FOLDER of files." & return & "(Files here that match the filenames in your CSV file" & return & "will be duplicated to a new TARGET location.)" without invisibles
	--display dialog "You have chosen SOURCE FOLDER:" & return & Source_Folder -- this is just feedback and can be deleted
	set Source_Folder_full_list to name of entire contents of Source_Folder
	
	--CODE for choosing your TARGET:	
	set Target_Folder to choose folder with prompt "Choose a TARGET FOLDER to duplicate the SOURCE files to." & return & return & "The count of files to be duplicated is " & return & (count of Source_List) & return & "Please check the folder when done." without invisibles
	--display dialog "You have chosen TARGET FOLDER:" & return & Target_Folder -- this is just feedback and can be deleted
	
	
	
	--Here's the meat of it:
	repeat with i from 1 to count of items in Source_Folder_full_list
		
		
		if (i as string) does not end with ":" or (aItem as string) does not start with "." then --check for folders and invisibles (want to ignore those)
			
			
			set file_source_OneNameAlias to (item i of Source_Folder) as alias
			set file_source_OneName to (item i of Source_Folder_full_list)
			--set Source_Folder_full_list to Source_Folder_full_list as string
			repeat with i from 1 to count of items in Source_List
				if file_source_OneName = item i of Source_List then
					duplicate file file_source_OneNameAlias to Target_Folder with replacing
					display dialog "The file " & return & return & file_source_OneName & return & return & "was duplicated." giving up after 1
					exit repeat
				end if
			end repeat
			
			
			
		end if
		
		
	end repeat
	beep 1
	display dialog "All files were duplicated as requested." giving up after 3
	
end tell





Hi, ArtooMetoo. Welcome to MacScripter.

Your repeat variable ā€˜i’ is being incremented here through the number of item names in the entire contents of the source folder ā€ ie. including the subfolders ā€ whereas the line only refers to items in the immediate top level of the folder. The error occurs when the value of ā€˜i’ exceeds the number of top-level items. (Also, of course, ā€˜i as string’ cannot possibly end with ā€œ:ā€ or start with ā€œ.ā€.)

You should really set ā€˜Source_Folder_full_list’ to the files of the entire contents, rather than their names. Then, in the repeat, work through this file list, getting the names as you go. I’ve modified your script according to theory, but haven’t tested the result. The way it is at the moment, if two or more of the files have the same name, the ones nearer the beginning of the list will be overwritten at the destination by the later ones. It’s also possible that ā€˜entire contents’ will choke if there are too many items for it in the hierarchy.


--Sur la Table script for copying files to a new location using a CSV file as source.

--This script assumes the following:
--  The CSV file contains no empty cells and no erroneous filenames
--  The files indicated in the CSV file are actually located in the SOURCE folder


tell application "Finder"
	activate
	
	--CODE for getting the INFO from a file
	set CSV_source_File to choose file with prompt "Select the CSV file containing the filenames you need to find and duplicate." without invisibles
	set OldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {return} --this MUST be return when using CSV files
	
	--CODE telling the script to make a list from the selected file.
	set Source_List to every text item of (read CSV_source_File as string)
	set AppleScript's text item delimiters to OldDelims
	
	--CODE for choosing your SOURCE:
	set Source_Folder to choose folder with prompt "Choose SOURCE FOLDER of files." & return & "(Files here that match the filenames in your CSV file" & return & "will be duplicated to a new TARGET location.)" without invisibles
	--display dialog "You have chosen SOURCE FOLDER:" & return & Source_Folder -- this is just feedback and can be deleted
	set Source_Folder_full_list to files of entire contents of Source_Folder
	
	--CODE for choosing your TARGET:	
	set Target_Folder to choose folder with prompt "Choose a TARGET FOLDER to duplicate the SOURCE files to." & return & return & "The count of files to be duplicated is " & return & (count of Source_List) & return & "Please check the folder when done." without invisibles
	--display dialog "You have chosen TARGET FOLDER:" & return & Target_Folder -- this is just feedback and can be deleted
	
	
	
	--Here's the meat of it:
	repeat with i from 1 to (count Source_Folder_full_list)
		set file_source_One to item i of Source_Folder_full_list
		set file_source_OneName to name of file_source_One
		
		if (file_source_OneName is in Source_List) then
			duplicate file_source_One to Target_Folder with replacing
		end if
		
	end repeat
	beep 1
	display dialog "All files were duplicated as requested." giving up after 3
	
end tell

Nigel,

Thanks so much for your helpful reply!

Sure enough, your script does work. I knew there was some logical step I was missing.

You rock.

–Mike