Find files from csv list, copy to a single folder

Hello,

I searched the forums to see if there were any posts on this already. There were some that were quite close to what I’m looking to do. However, they were different enough such that I was not able to make them work.

So I have about 150 pictures that reside one 1 of 4 volumes. Actually, there are over 600 pictures on the drives - I only need to treat 150 +/- of them. The files in question are in various spots on those drives. I can generate a list of the files that are of interest. The list of 150. So this list could be a single column in an Excel spreadsheeet. It can be a CSV file - whatever is easiest. I can generate the list and tweak it to make it function the best for the script.

Here’s my goal: I want to find the 150 files of importance - from my list of filenames. Then I want to copy these found files all into one folder. This recipient folder can live on the desktop - or where ever is best. Eventually it’s going to get copied to another machine anyway. So this temporary folder location is irrelevant basically.

I found this script which is quite similar to what I need. The primary difference is that it makes individual folders for the files in question, whereas I will have all the files copied into a single folder. But the shell scripts in the examples through me off. I’m not savvy at all, regarding the shell scripts. But this particular example was very similar to my goal.

This query was asking a very similar question. I don’t need to do the renaming they mentioned. Anyway, the OP said they found the solution they needed. Too bad they did not post it here.

So again my steps are:

1.) Get filename from list
2.) Copy the found file from it’s location to a (single) folder.
3.) Repeat with next list item.

One caveat too, I suppose. I need some fault tolerance. If it doesn’t find an item with the exact name in question - I need it to move on. Because I may have an errant typo or two in the “list” of filenames that I’m going to generate.

I’ll add that I don’t like posting here without including some scripting of my own - to prove I am doing some work on the project in question. So please forgive the fact that I’m not doing so now. I just didn’t know where to really start.

Any input would be greatly appreciated !! Thanks.

Is the file going to have posix or finder style paths?

Figure that out and the rest is pretty easy. Also while either a excel or csv would work I think csv would be easier and faster.

What’s the difference between POSIX & Finder paths? I’ve never used a POSIX path before.
Will this be dictated by the fact that the files in question are on an external volume? Or will that even matter?

Thanks.

Well lets assume the file name is Image1.jpg and it’s sitting on the external volume named ExtDisk. Which does your path look like?

ExtDisk:Image1/jpg
/Volumes/ExtDisk/Image1.jpg

Make your text file HFS paths - return delimited like so.

Then run this script.


--create a folder on your desktop called "Temp"
set destFolder to (path to desktop as Unicode text) & "Temp:" as string

--path to text file - I put mine on the desktop and called it paths.txt
set txtFile to (path to desktop as Unicode text) & "paths.txt" as string


--put the path to the cvs txt file here
set filePaths to paragraphs of (read file txtFile)

repeat with i from 1 to count of filePaths
	set thisFile to item i of filePaths as alias
	tell application "Finder"
		duplicate thisFile to destFolder
	end tell
end repeat

Cheers,

Craig

Well the trick I suppose then, is going to be fetching the complete file path names. See, I won’t necessarily have that - precisicely. Well I can figure it, out of course…but I was hoping that the script will search subfolders. My folder structure would be something like this:

/Voumes/RAID_4/Viking Media/Viking/Folder 1/Image_0001.tif
or
Voumes:RAID_4:Viking Media:Viking:Folder 1:Image_0001.tif

The issues is that where I say “Folder 1” here - there are 27 folders. The 150 pictures will live in one of these 27 folders. But I’m hoping to avoid having to completely specify that file path. If I go that far - and know the exact file path - then I may as well drag-&-drop by then.

So basically, I know that all the pictures are in the “Viking” folder, but they’re really inside another folder inside this “Viking” folder. So I’m hoping the script can search the subfolders. Is this possible?

Thanks for the input so far.

Actually, StafanK’s script works very nicely with just two minor changes.


tell application "Microsoft Excel" to tell active sheet
	set L to value of used range -- get the search terms
end tell

set inputFolder to quoted form of POSIX path of (choose folder) -- the folder to search in (recursive)

repeat with i from 1 to count L
	set keyword to item 1 of item i of L
	-- search with find
	-- set foundFiles to paragraphs of (do shell script "find " & inputFolder & " -type f -name '*" & keyword & "*' ! -name '.*'")
	-- search with mdls
	set foundFiles to paragraphs of (do shell script "mdfind -onlyin " & inputFolder & " 'kMDItemFSName  = \"*" & keyword & "*\"'")
	--set dest to do shell script "mkdir -v ~/Desktop/" & keyword -- makes new folder at desktop
	set dest to POSIX path of (path to desktop as Unicode text) & "Temp" as string
	repeat with i in foundFiles
		try -- skips folders (only needed with the spotlight search)
			do shell script "cp " & quoted form of i & space & quoted form of dest
		end try
	end repeat
end repeat

So then, given my file/folder structure, if when prompted by your script to “choose folder”, I select my “Viking” folder, will the script proceed to search through the 27 folders inside this “Viking” folder?

Yes. That is what it does.

In sort of a preliminary response to my own last post I’ve found two deal-breakers with the script as it is:

1.) It does not seem to be able to search inside of subfolders. I have to point it to the EXACT folder to make it work.
2.) After pointing it to the proper folder, it copies all of the files in that folder to the “Temp” folder on the desktop, not just the image I’m supposed to be isolating.

Thanks again for the input thus far…

OK…so I’m wrong regarding issue #1.

When trying to run it again, it does indeed seem to look inside the subfolders.

However, issue #2 is still a problem. Because, for whatever reason, it’s NOT solely copying the single file (that I’m using to test with) but seems to be trying to copy ALL the folder contents still. It’s finding my picture - and all others too.

Post a few lines from your Excel file.

Well for the sake of simplicity, right now I’m only using a single-cell Excel file.

So, cell A1’s contents is the filename: Helen as a kid021.tif.

This is the exact name of one of the files I’d be looking for. But it starts copying all the files to the “Temp” folder. Hope that helps.

Try this. I am out the door and won’t be back till later this evening.

hth,

Craig


tell application "Microsoft Excel" to tell active sheet
	set L to value of used range -- get the search terms
end tell

set inputFolder to quoted form of POSIX path of (choose folder) -- the folder to search in (recursive)

repeat with i from 1 to count L
	set keyword to item 1 of item i of L
	set foundFiles to paragraphs of (do shell script "find " & inputFolder & " -type f -name '" & keyword & "' ! -name '.*'")
	
	--a little prevention - remove when everything is working
	if length of foundFiles > 1 then
		display dialog "More than one found."
		return
	else
		display dialog item 1 of foundFiles
	end if
	
	set dest to POSIX path of (path to desktop as Unicode text) & "Temp" as string
	repeat with i in foundFiles
		try -- skips folders (only needed with the spotlight search)
			do shell script "cp " & quoted form of i & space & quoted form of dest
		end try
	end repeat
end repeat

Craig,
Thanks for the input. I’ll be doing more testing on this very soon. But preliminarily I can say that it did NOT work for me if the excel list had more than a single entry. I’ll be reporting back with more details in the very near future. Thanks for your help thus far. It’s greatly appreciated !!
Kevin

Hello guys,
I have the exact same situation going on in my workflow… did any of you find a “Final” solution to this issue? It would be a shame if this topic should not reach a final solution…
Thanks anyway