Search for folder name on volume and move files

Hi!

I am working on organizing our files on our servers and I want to move folders from one volume to another. I would like this to be automated so that when folders are dropped into my source folder ‘Archive’ they will automatically be moved over to our new volume.

The folders on both volumes will have the same names.
Source folder: Users:UserA:Desktop:Archive:ABC_123
Destination volume: Volumes:Production:Titles: (then it needs to find the right folder in here)

eg: folder name “ABC_123” has subfolders within named “ABC_123_low” and “ABC_123_high”.
I would like it to search my destination volume for the folder named “ABC_123” and move “ABC_123_low” and “ABC_123_high” into that folder.

Thanks for the help, I am new at scripting and I have been searching for forums for moving files but really haven’t found one that will do the search and move as I was hoping to do it.

Soo, when you drag a folder named ABC123 into the folder “source/archive”, then you want the subfolders of ABC123 to be moved to “destination/ABC123”?
And the subfolders of ABC123 have the files?

Please make another example then I’ll see what I can do.

Yes thats it exactly! Is this the information you need?

Folder:
ABC123
Subfolders:
ABC123_low
ABC123_high

Source Folder location:
Macintosh HD:Users:UserA:Desktop:Archive:

Destination Where folders get moved to:

Find folder named ABC123 within Volumes:Production:Titles:

and move folders ABC123_low, ABC123_high into folder ABC123

Thanks!

Courtney

Ok, here it is. Try to understand it, that will help you more than just using it.
The actual moving around is done with a shell command because it’s faster and easier.

on adding folder items to thisFolder after receiving addedItems
	set inputPath to "path:to:folder"
	set outputPath to "path:to:folder"
	-- get the name of the folder
	set theFolder to (addedItems as text)
	tell application "Finder" to set droppedFolder to the name of the folder theFolder
	-- get the paths in POSIX
	set origPath to (inputPath & ":" & droppedFolder)
	set sourcePosix to the POSIX path of origPath
	set destPosix to the POSIX path of (outputPath & ":" & droppedFolder)
	-- see if dest-folder exists, if not -> create
	set folderExists to false
	tell application "Finder" to if exists destPosix as POSIX file then set folderExists to true
	if not folderExists then do shell script ("mkdir " & destPosix)
	-- move
	do shell script "mv " & sourcePosix & "/* " & destPosix & "/" -- does: mv source/folder/* destination/folder/
end adding folder items to

Set “inputPath” and “outputPath” at the top (inputPath = the drop box where you drag folders to, outputPath = the destination folder).

If you drag the folder “bla” with n subfolders and files, the script checks if “destination/bla” exists, if it doesn’t, it creates it, then moves everything contained in “bla” over there. If the folder “destination/bla” already exists, it just moves the contents.

To use the script:

  • open the script editor
  • paste it and save it in MacHd/Library/Scripts/Folder Action Scripts/nameofscript.scpt
  • add the script as a folder action on your folder where you drag things (if you don’t know how to do this: link)

Let me know if you’re having trouble.

Cheers

Hi Phore,

This looks good, I will test it out in a minute but I have a question first-

If i don’t know the exact output path location how will it know how to find it?

I know that the output folder structure starts with Volumes:Production:Titles:
but then there are a bunch of subfolders within Titles and the folder will not be right under Titles but could be 4 subfolders underneath.

Is there a way to search for the “droppedFolder” under Volumes:Production:Titles and then set that output path location dynamically since the outputfolder location will change with each new folder I add?

Hope that is clear, your code may do this already, but I just wanted to make sure I explained the folder structure properly since you asked me to read it through first and understand it before i tried it out :slight_smile:

Thanks!

Courtney

Ah, no it does not. It expects the folder to be in the root of “destination/”.
I put in a shell “find” command now instead. It now searches for the exact name of the dropped folder in the destination folder and returns it’s path:
→ But if the destination/dropped_folder doesn’t exist, the script will fail now instaed of create it.
Is that ok? Or does it need to create folders sometimes?

on adding folder items to thisFolder after receiving addedItems
	try
		set inputPath to "path:to:folder"
		set outputPath to "path:to:folder"
		-- get the name of the folder
		set theFolder to (addedItems as text)
		tell application "Finder" to set droppedFolder to the name of the folder theFolder
		-- get the paths in POSIX
		set origPath to (inputPath & ":" & droppedFolder)
		set sourcePosix to the POSIX path of origPath
		-- find the output-folder and use it as path
		set tmpPath to the POSIX path of outputPath
		set destPosix to do shell script ("find " & tmpPath & " -name \"" & droppedFolder & "\" -type \"d\"") 
		-- does: [find /wheretolook/ -name "name_of_folder" -type "d"] (type: directory)
		-- move
		do shell script "mv " & sourcePosix & "/* " & destPosix & "/"
		-- does: mv source/folder/* destination/folder/
	end try
end adding folder items to

The script is a bit slower because of the find command.

Btw: If the folder contain alot of big files, you might wanna reconsider using “on dragging into a folder” to kick off your script.
Sometimes the files might still be copying themself into the dropbox and the script will already think it has to start.

Phore,

Woo excellent, trying it out now. I think it might be good to create a new folder instead of failing, it would make the process even more automated and less likely to fail?

I will see how the script works and see if it needs to have the 'on dragging into a folder" added to it. (Sounds logical, will probably ask how that is done later also!)

Thanks a bunch and will let you know how it goes!

Sincerely,

Courtney

Don’t you know if those folders are there? If they are, then you shouldn’t have any problems.
The reason I didn’t include creating-a-folder in the script is, that I don’t know where the folder would supposed to be created.
In the root of the destination folder?

You miss-understood the part about “on dragging into folder”, but it doesn’t matter as of now.
Let me know if the script works otherwise we’ll talk about this stuff again.

Signing off,
phore

Phore,

The folders would be created by other people and would require us to check the volume each time to see if the folder was created before moving anything first. Creating the folder at the destination would indeed by a complicated thing to create - the folder structure isn’t on the destination root - its about 4 sub levels down and different for each folder.

so if the source folder was named AHL_SS_0809_DEC, the destination folder structure would look like this:

AHL
→ AHL_SS
-----> AHL_SS_0809
---------->AHL_SS_0809_DEC
-------------> (with the two moved folders residing here)

Thanks and let you know soon how it works!

Courtney

Browser: Firefox 2.0.0.9
Operating System: Mac OS X (10.4)