finding and duplicating files

Hi Folks-

I’m trying to write a script that will search through a bunch of subfolders, pull out all PDFs and duplicate them somewhere else. I also later want to note those folders that do not have any PDFs in them, but I’m having trouble getting the first bit to work! InTest has 2 subfolders.

I tried it both with the ‘cp’ line command and the duplicate command. Neither works.

tell application "Finder"
	set rootin to "MrComputer:Users:a351958:Desktop:InTest:"
	set rootout to "MrComputer:Users:a351958:Desktop:Outtest:"
	set FolderList to get the name of every folder of (rootin as alias)
	repeat with thisfolder in FolderList
		set subfolder to rootin & thisfolder
		set pdflist to (every file of entire contents of (subfolder as alias) whose name ends with ".pdf")
		repeat with thispdf in pdflist
			set pdfname to (thispdf as alias)
			set pdfname to name of pdfname
			set pdfin to rootin & pdfname
			set pdfin to POSIX path of pdfin
			set pdfout to rootout & pdfname
			set pdfout to POSIX path of pdfout
			do shell script "cp " & quoted form of pdfin & " " & quoted form of pdfout
		end repeat
	end repeat
end tell

gives the error

"no such file or directory

while

tell application "Finder"
	set rootin to "MrComputer:Users:a351958:Desktop:InTest:"
	set rootout to "MrComputer:Users:a351958:Desktop:Outtest:"
	set FolderList to get the name of every folder of (rootin as alias)
	repeat with thisfolder in FolderList
		set pdflist to (every file of entire contents of thisfolder whose name ends with ".pdf")
		repeat with thispdf in pdflist
			duplicate file thispdf to folder rootout
		end repeat
	end repeat
end tell

gives

can't get entire contents of subfolder 1

thanks for your help.

-Ralph

Hi Ralph,

probably this is the shortest and fastest solution,
it uses the find command of the shell which can pipe the result directly to the copy command


set rootin to POSIX path of "MrComputer:Users:a351958:Desktop:InTest"
set rootout to POSIX path of "MrComputer:Users:a351958:Desktop:Outtest"
do shell script "find " & quoted form of rootin & " -name '*.pdf' -exec cp {} " & quoted form of rootout & " \\;"

StefanK-

that’s amazingly compact and elegant. Can you tell me why my scripts errored out?

thanks,

Ralph

will our line commands ever differ significantly because we’re set up with BASH shells?

-Ralph

first script:
wrong reference


.
set pdfin to subfolder & ":" & pdfname
.

second script:
you gather the name of the folders but you need the file specifiers of the folders


.
set FolderList to get every folder of (rootin as alias)
.

aha. now I get it. The following works (though not as elegantly as Stefan’s code)

tell application "Finder"
	set rootin to "MrComputer:Users:a351958:Desktop:InTest:"
	set rootout to "MrComputer:Users:a351958:Desktop:Outtest:"
	set FolderList to get every folder of (rootin as alias)
	repeat with thisfolder in FolderList
		set pdflist to (every file of entire contents of thisfolder whose name ends with ".pdf")
		duplicate every item in pdflist to folder rootout
		
	end repeat
end tell

thanks again!

Ralph

A final note, Ralph, the argument of the duplicate command can be a list,
so this is sufficient

 duplicate pdflist to folder rootout