Applescript to copy files while preserving directory structure

I am trying to copy project files from my Project Drive to my Archive Drive and split the files into 2 category folders. But, I want to keep the original directory structure of the file when I copy it so I can avoid remaking a ton of folders. Both drives are local and I want to retain the original file info (Date created, etc.)

I’ve had success with this Rsync command:

rsync -Rra <source> <destination>

It copied files and folders while retaining the file info. But, I want to select multiple files then trigger the automation from a keyboard shortcut or the context menu, so I need to put it in an AppleScript. I know I can use Automator to get the selected files, but I don’t know how to do that in AppleScript…

The original folder structure is like this:

Project Drive
—Project Folder
-----Assets Folder
-------AlphaFile_1
-------AlphaFile_2
-------BetaFile_1
-------BetaFile_2

I want to copy the files to Archive Drive so they’ll look like this:

Archive Drive
------Alpha Folder
---------Project Folder
------------Assets Folder
---------------AlphaFile_1
---------------AlphaFile_2
------Beta Folder
---------Project Folder
------------Assets Folder
---------------BetaFile_1
---------------BetaFile_2

Thanks for any help!

I have two suggestions. The first script replicates the full path of a source file in the target folder. The second script replicates only a portion of the path of a source file in the target folder. As currently written, the excluded portion of the path is /Users/Username/.

set targetFolder to "/Volumes/Store/Alpha Folder/" -- change to desired value

tell application "Finder" to set sourceFiles to selection as alias list

repeat with aFile in sourceFiles
	set contents of aFile to (quoted form of POSIX path of aFile) & space
end repeat

do shell script "rsync -Ra " & sourceFiles & space & quoted form of targetFolder
set targetFolder to "/Volumes/Store/Alpha Folder/" -- change to desired value
set pathPrune to "/Users/Username/" -- change to desired value
set pruneCount to (count pathPrune)

tell application "Finder" to set sourceFiles to selection as alias list

repeat with aFile in sourceFiles
	set pFile to POSIX path of aFile
	if pFile begins with pathPrune then
		set pFile to pathPrune & "./" & text (pruneCount + 1) thru -1 of pFile
		set contents of aFile to quoted form of pFile & space
	else
		display alert "The path of a file could not be pruned." message pFile
		error number -128
	end if
end repeat

do shell script "rsync -Ra " & (sourceFiles as text) & space & quoted form of targetFolder

Both of these scripts would benefit from additional error correction.

This is awesome Peavine! I’m looking at it now.

To answer your questions:

I did want to keep the entire original directory structure because I thought it would make it easier to write the script. But, your instincts were correct and your idea is way better.

Regarding criteria that determines whether to copy a file to the Alpha or Beta folder, I am going to choose manually.

Regarding selecting files: yes, I meant files I select in a Finder window.

I will be mindful of these details for future posts.

I’ll report back after I check out the script!