Building Dynamic Folder Paths

Hi folks.

Now I’m moving files around. I need to set destination folders based on an array I’m getting elsewhere, and I’m having a bit of an issue building dynamic references to directories I know are there. You know that point you have 17 scripts open in Script Editor and you’re completely lost? That’s where I am.

So let me describe it this way. sourceFolder, destFolder. Child folders are duplicates inside both. That is true always.

[format]sourceFolder
alpha
bravo
charlie
destFolder
alpha
bravo
charlie[/format]

I have built an array of the folder names in sourceFolder. I want to iterate through those, and send the internal files in each, to the corresponding target folder in destFolder.

This is where I am now with the code:


set sourceFolder to choose folder
set destinationFolder to choose folder

tell application "Finder"
	repeat with aFolder in (get folders of sourceFolder)
		
		set filesToMove to (files of aFolder)
		set mydest to (destinationFolder & aFolder as POSIX file)    -- ERROR HERE
		log mydest
		move filesToMove to mydest
		
	end repeat
end tell

I’m confused around the need for POSIX for building folder paths, and how I can avoid that. I’d like to hard wire the sourceFolder and destinationFolder paths instead of the GUI dialog boxes as well, but that too confused me with the POSIX need.

Do non-POSIX paths need “:Volumes:myVolume:” as a prefix to “:Users:myuser:…”?

Anyway, a couple questions in there. I’m sure it’s slippery, but I don’t have a quick reference that I can find any of this in, and earlier answers on the Google don’t work, probably due to version discrepancies. I am on El Capitan, BTW. Cheers

Usually hits me around three…

But you may be over-thinking it. You don’t need to use POSIX paths for this, or even to know how a file path is constructed, because Finder knows the names of the folders, and how to convert the aliases returned by choose folder into Finder references, and does pretty much all of it for you:

tell application "Finder"
    --define source folder
    set sourceFolder to (choose folder)
    --define destination folder:
    set destFolder to (choose folder)
    --get a list of folders in sourceFolder (as Finder references):
    set outFolders to folders of sourceFolder
    
    --loop through the source folders, get their names, send their files to folders of the same name in the destination folders:
    
    repeat with eachFolder in outFolders
        set outName to name of eachFolder
        move files of eachFolder to folder outName of destFolder
    end repeat
end tell

I haven’t built in any error correction, so if the folders in sourceFolder don’t match those in destFolder, or if files of the same name exist in the target folders, you’ll get errors. But hopefully that gives a general idea.

Cheers,

H

That’s not necessary. For example, the following is the HFS path to the Documents folder on my boot drive:

Macintosh HD:Users:Robert:Documents:

BTW, the Finder works well when moving a small number of files but can be slow or even fail otherwise. One alternative that works well with a very large number of files is the rsync utility:

set sourceFolder to quoted form of POSIX path of (choose folder)

set targetFolder to quoted form of POSIX path of (choose folder)

do shell script "rsync --archive --remove-source-files " & sourceFolder & space & targetFolder

A few comments:

  • Rsync creates the destination folders if they don’t exist.
  • It overwrites existing files, although there are options to modify this behavior.
  • As written above, it acts recursively, moving all files in the target folder and all of its subfolders.
  • Care needs to be exercised not to select the wrong source folder.

Using the rsync shell command is more convenient than creating an identical folder structure manually. But, suppose you insist on your approach. Then:

  1. What you call “non-POSIX paths” above is known as HFS paths
  2. the Finder works with HFS paths fine, so you original script above need only little tweaking:

set sourceFolder to (choose folder) as text -- hfs path
set destinationFolder to (choose folder) as text -- hfs path

tell application "Finder"
	repeat with aFolder in (get folders of sourceFolder)
		
		set filesToMove to (files of aFolder)
		set mydest to (destinationFolder & aFolder) -- hfs path
		log mydest
		move filesToMove to mydest
		
	end repeat
end tell

NOTE: in the set mydest… code line Finder reference aFolder implicitly coerced into text by the AppleScript, since the left side of the concatenation is text (HFS is text). Conclusion: building dynamic folder path here is better using HFS paths

Hi there. Ya, choose folder… is what I’m avoiding. Dynamic in the sense that the source child folders is the array to traverse, then parse out the child folder names, then apply them to a destination parent folder. So there is no choose at all. Automation is the key here.

source:
mainsource:alpha:
mainsource:bravo:
mainsource:charlie:

dest:
maindest:alpha:
maindest:bravo:
maindest:charlie:

Sources are in an array, so as I go through them, I need to target the dest version.

Yes I automate rsync. I want to achieve this all as simple as possible as the first part of this workflow is in AS. The first part has many files copied and it’s just fine. I just need to re-move those files yet again.

Sorry, I posted before finishing reading. I should stop doing that. Your inner 4 lines are what I’m looking for. I tried something like that, messing it up with POSIX, but I will give this HFS+ version a try.

Cheers

OK I’m getting an error with hard set folder HFS paths and the child folder names inside the source:


tell application "Finder"
	set mys to "Flocky:Users:rich:master:VBR:"
	set myd to "Flocky:Users:rich:master:normal:"
	
	repeat with thisFolder in (get folders of mys)
		set filesToMove to (files of thisFolder)
		set mydest to (myd & thisFolder) -- hfs path
		log mydest
		move filesToMove to mydest
	end repeat
	
	beep
end tell

Here’s the error:

Can’t get every folder of “Flocky:Users:rich:master:VBR:”.

I have no clue why it can’t get child foldernames in that HFS reference.

Cheers

This worked on my computer (with different paths). I added folder before mys and mydest. I also changed the line beginning “set mydest” so that the path is correct.

set mys to "Flocky:Users:rich:master:VBR:"
set myd to "Flocky:Users:rich:master:normal:"

tell application "Finder"
	repeat with thisFolder in (get folders of folder mys)
		set mydest to myd & (name of thisFolder) & ":"
		move (files of thisFolder) to folder mydest
	end repeat
	beep
end tell

Boom. Worked. folderName was the suffix that was needed. The whole path was doubling up.

Nicely done. Cheers

It’s because you’re asking for the files of a string, not a folder.

That was my confusion on HFS notation vs POSIX.

I forgot in 1 code line HFS coercion to Finder folder coercion. Correct script is this:


set sourceFolder to (choose folder) as text -- hfs path
set destinationFolder to (choose folder) as text -- hfs path

tell application "Finder"
	repeat with aFolder in (get folders of folder sourceFolder) -- EDITED
		
		set filesToMove to (files of aFolder)
		set mydest to (destinationFolder & aFolder) -- hfs path
		log mydest
		move filesToMove to folder mydest -- EDITED
		
	end repeat
end tell

Or, using hardcoded paths like in your last script above:


set mys to "Flocky:Users:rich:master:VBR:" -- hfs path
set myd to "Flocky:Users:rich:master:normal:" -- hfs path

tell application "Finder"
	repeat with thisFolder in (get folders of folder mys) -- used coersion HFS to Finder folder mys
		set filesToMove to (files of thisFolder)
		set mydest to (myd & thisFolder) -- hfs path
		log mydest
		move filesToMove to mydest
	end repeat
end tell

beep

Ya we’re all good. This export/conversion/consolidation/copy to thumb process is over 2 hours long.

But all good! Cheers!

KniazidisR. Neither of your scripts worked for me. Your first script worked if I changed the lines beginning with “set mydest” and “move files” as follows:

set sourceFolder to (choose folder) as text -- hfs path
set destinationFolder to (choose folder) as text -- hfs path

tell application "Finder"
	repeat with aFolder in (get folders of folder sourceFolder) -- EDITED
		
		set filesToMove to (files of aFolder)
		set mydest to destinationFolder & (name of aFolder) & ":" -- hfs path
		log mydest
		move filesToMove to folder mydest
		
	end repeat
end tell

Similar changes to the second script also seem necessary.

Hi, Peavine. You are right, partly.

The AppleScript is smart and automatically coerce aFolder to format of left operand of & operation, as I sad above. So, the move command line only needed tweaking from your side.

I have no doubt that you gave the exact answer to OP in your script, not me. I just didn’t want to test my scripts, because I didn’t want to move anything on my computer, this time.

Sure the code line move filesToMove to mydest doesn’t work as is?

UPDATE: I tested right now. Finder is not as smart as I expected. :smiley: