Copy new files in folder to another folder on another server.

Hi,

I am a newbie to this but am looking for a script that will automatically copy files that are placed into a folder into another folder on another server. I’d like to use a script in conjunction with folder actions to do this.

Any help would be greatly appreciated.

Thanks.

Josh

what do you have so far?

Let us know and we can help plug in the holes.

Thanks for the reply. This is what I have so far.

tell application "Finder"¨duplicate (every file of folder “Path:to:source”) to folder “path:to:dest:” with replacing¨end tell

I want to have this functionality assigned to a “hot” folder as an folder action and that is what I am working on now. If I manually enter in my source and destination into this script, it works but I’d like it to be automated when files are placed into the input folder. What I’d really like is for it to read part of the file name (job number) and put it into a folder with those same characters (job number). Basically, I need these files to be copied to the appropriate job number folder in our library on a remote server.

Thanks for any help you can offer!

Josh

Time is almost done at work so lets see if I can get you started in the right direction…

Okay this should server as an idea for how to work with folder actions and the on add action


on adding folder items to thisFolder after receiving theFiles
	repeat with thisFile in theFiles
		tell application "Finder" to duplicate file thisFile to folder "path:to:dest:" with replacing
	end repeat
end adding folder items to

From there its a matter of determining the files proper location before actually doing the duplicate… this can happen a number of ways depending on what your file naming conventions are.

Thanks so much! I’ll work on this tonight.

That works perfectly, myndcraft! I set up a folder action with this script, modified the destination, and it is just what I need. Thank you.

My next more advanced step is to have this read the job number in the file name and put it in the appropriate subfolder with this same job number in its name. I’ll look into this.

An example of our naming convention is as follows:

I’m now working on having the script place a copy of the following file in a subfolder named QLMH0110.
4098_QLMH0110.pdf

this file into a subfolder named QLMH0131, etc., etc.
3568_QLMH0131.pdf

If you have any insight on this (and time, of course), that would be appreciated. Again, I’m very new to this so thanks so much for your assistance!

Josh

It appears that the following script may help me achieve my goal. I’m getting caught up set folder_name line:
set folder_name to words -8 thru -6 of the_file_name as string

I want it to read the QLM0110 (and .pdf is ok) from this filename 4098_QLMH0110.pdf

Here is the script…

property photos_archive : “Macintosh HD:Users:yourusername:Desktop:photos:”

on adding folder items to this_folder after receiving theFiles
repeat with thisFile in theFiles
tell application “Finder”
set pathname to thisFile as alias
set the_file_name to thisFile as string
set AppleScript’s text item delimiters to “-”
set folder_name to words -8 thru -6 of the_file_name as string

		if folder named folder_name in folder photos_archive exists then
			move thisFile to folder folder_name of folder photos_archive replacing yes
		else
			set pub_date_folder to make folder in folder photos_archive
			set name of pub_date_folder to folder_name
			move thisFile to folder folder_name of folder photos_archive replacing yes
		end if
		
		set AppleScript's text item delimiters to ""
	end tell
end repeat

end adding folder items to

Thanks again for any help!!!

Many ways to do this and I generally prefer to do things quick and dirty the first time and then refine the process, but we need to know some things about your work flow… What are the constants?

Will a file name ALWAYS follow the convention of

####_XXXX####.pdf

if it is then something as simple as


on adding folder items to thisFolder after receiving theFiles
	repeat with thisFile in theFiles
		set folname to text -12 through -5 of thisFile
		tell application "Finder" to duplicate file thisFile to folder ("path:to:" & folname) with replacing
	end repeat
end adding folder items to

will give you the folder name to reference on remote volume. Of course something that doesn’t follow the above convention will immediately cause problems. That being the case we could then start looking at some means of intelligently extracting the folder name using text item delimiters along the lines of what you posted above.

Thanks again for your efforts!

To answer your question, yes, the filename will always follow this convention:
####_XXXX####.pdf

This appears to be just what I am looking for. I have edited your script with the appropriate path to the subfolders that this will look for but, for some reason, it is not working.

My current script is as follows:

on adding folder items to thisFolder after receiving theFiles
repeat with thisFile in theFiles
set folname to text -12 through -5 of thisFile
tell application “Finder” to duplicate file thisFile to folder (“PDF:QLM:QLMH_Library:” & folname) with replacing
end repeat
end adding folder items to

My test file is named 0000_QLMH0110.pdf and I am dropping it into the folder with this action assigned to it. I have a subfolder named QLMH0110 in the PDF:QLM:QLMH_Library folder and want it to copy this file into this subfolder. It seems like it should work but I must be overlooking something.

Can you explain this line?
set folname to text -12 through -5 of thisFile

Again, I’m waiting to receive my AppleScript books…
I would guess that the -12 refers to 12 places from the end of the filename to 5 places from the end of the filename. I could be completely off though. It this is right though, it should be picking up the QLMH0110 from the filename and looking for the corresponding subfolder.

I’ll keep working on this one!

Thanks again for all of your help.

Josh

When you have any string “HereIsSomeComplicatied String with spaces”, you can recover pieces of it by specifying text a thru b of theString.

if a or b are positive integers, the count is from the front of the text, and if negative integers, from the end of the text backward.

set f to "0000_QLMH0110.pdf"
text -12 thru -5 of f --> "QLMH0110"
-- or
text 6 thru 13 of f --> "QLMH0110"
-- or
text -12 thru 13 of f --> "QLMH0110"
-- or
text 13 thru -12 of f --> "QLMH0110"

And a bit more:

set s to "HereIsSomeComplicated String with spaces"
set o to offset of "with" in s --> 30; the 30th text is "w", 3 more get "ith".
text 30 thru 33 of s --> "with"

set o to offset of "Some" in s --> 7
text 7 thru 10 of s --> "Some"

-- As you see, it's inclusive. You can also go for words:
word 2 of s --> "String"
word -3 of s --> "String"

set f to "0000_QLMH0110.pdf"
word 3 of f --> "QLMH0110" -- but be careful. "_" is considered a word but
word 4 of f --> "pdf"

Thanks for the input, Adam. The first of your explanations makes perfect sense and I’ll work on understanding the second (newbie). With this first set of rules it seems that my current configuration should work but it isn’t. I’ll keep working at it though. Thanks again.

The following line should copy the filename 0000_QLMH0110.pdf to a subfolder named QLMH0110 in the following path… or am I missing something? It doesn’t seem to be working. Again, I have assigned this script to the input folder.

tell application “Finder” to duplicate file thisFile to folder (“PDF:QLM:QLMH_Library:” & folname) with replacing

Thanks!

Josh

Try making both references aliases (don’t know what thisFile is, but if not alias, make it one):

tell application "Finder" to duplicate file alias thisFile to folder alias ("PDF:QLM:QLMH_Library:" & folname) with replacing

This works for me:

set thisFile to (choose file) -- an alias
set thisFolder to (choose folder) -- an alias
tell application "Finder" to duplicate file thisFile to folder thisFolder with replacing

Thanks, Adam. I’m hoping that my AppleScript and Automator books will come in today!
I’m struggling a bit to understand how to take advantage of your last reply.

I had success with this script that you provided:

set thisFile to (choose file) – an alias
set thisFolder to (choose folder) – an alias
tell application “Finder” to duplicate file thisFile to folder thisFolder with replacing

I’m trying to understand how to use the other script you supplied to make this an automated process without user interaction. Do I integrate it with the script I was working with yesterday? If so, this is what I have:

on adding folder items to thisFolder after receiving theFiles
repeat with thisFile in theFiles
set folname to text -12 through -5 of thisFile
tell application “Finder” to duplicate file alias thisFile to folder alias (“PDF:QLM:QLMH_Library:” & folname) with replacing
end repeat
end adding folder items to

This is not working for me either.

I apologize, Adam. I’m very new to this and, again, am waiting for my books. You’ve been a great help so far so thanks for that. If you can offer any other input, great. If not, that is ok too!

Josh

This works for me. You don’t need the aliases, I’ve moved the tell block outside it all, and I’ve inserted a try block so if something goes wrong, you can tell me what it said.


on adding folder items to thisFolder after receiving theFiles
	try
		tell application "Finder" to repeat with thisFile in theFiles
			set folname to text -12 through -5 of (name of (info for thisFile))
			duplicate file thisFile to folder ("PDF:QLM:QLMH_Library:" & folname) with replacing
		end repeat
	on error tError
		display dialog tError
	end try
end adding folder items to

Thanks Adam! This works perfectly. I changed it to read the numbers from the front of the filename. I may try to adjust the script so that it “reads” the QLMH0110 no matter where it is placed in the filename. Just in case the typical naming convention is not used. I assume that this is possible. Is it?

Thanks again.

Josh

Probably, but with so few examples of a file name, I can’t say for sure. Give us a sample of variations with more than one root name and I’ll be able to say more. It’s a matter of pattern recognition of one kind or another and may require a bit of regex to accomplish if the variations are huge, but many such things can be resolved quite easily using AppleScript’s text item delimiters or by noting that names are always a certain number of characters in length with the first four being from the alphabet and the last four numbers, or that the name is always preceded by an underscore and followed by a period, just for examples.

Understood Adam. Here is a list of some filename examples that I am working with now:

4013_QLMH0110.pdf
QLMH0110AA.pdf
QLMH0110BA_CO_BW_M.pdf
QLMH0130F.pdf
4092_QLMH0128.pdf
QLMH0120B.pdf

Thanks again.

Josh

So essentially the part you are trying to pull will always be in the format of xxxx#### or can the number of letters and numbers every vary? (not counting the other misc info in the filename)

this part will always be in the format of xxxx####

This works for me:


set FN to {"4013_QLMH0110.pdf", "QLMH0110AA.pdf", "QLMH0110BA_CO_BW_M.pdf", "QLMH0130F.pdf", "092_QLMH0128.pdf", "QLMH0120B.pdf"}

set delim to "QLMH"
set myNames to {}
set d to AppleScript's text item delimiters -- save previous values
repeat with N in FN
	set AppleScript's text item delimiters to delim
	set temp to text item 2 of N
	set AppleScript's text item delimiters to d -- set them back to what they were coming in
	set end of myNames to delim & text 1 thru 4 of temp
end repeat

ADD ON: if your files have several such “leaders” in their file names, then you’ll have to first distinguish the leader from the raw file names. Will that be the next problem?