Move files based on Folder Names

I’m brand new to Apple Script, so please say with me. I do know how to program some though so I’m not a total newbie.

Here is what I’m looking to do:

I have over 6000 folders with account documents in them, that I need to move to a different folder structure.
INPUT:
…/99196-Kennedy/{Account Docs}
…/FM026-Miller/{Account Docs}

OUPUT:
…/99196_3078/{Account Docs}
…/FM026_4440/{Account Docs}

The output folder structure is already built, as the 3078 in the output folder is a unique ID for a database we’ve built.

So I’m looking for a script that will move the files based on the Account Number(99196) from the INPUT folders to the OUTPUT folders.

Notes:
All Account numbers are 5 characters long.

Thanks all for your help, I’m really looking forward to getting up to speed with AppleScript and can’t wait till I can return the favor.

Hi,

assuming there is always a corresponding folder in output and the first 5 characters are unique,
try this


set inputFolder to choose folder with prompt "Choose input folder"
set outputFolder to choose folder with prompt "Choose output folder"
tell application "Finder"
	set allFolders to folders of inputFolder
	repeat with aFolder in allFolders
		set baseName to text 1 thru 5 of (get name of aFolder)
		set destinationFolder to (1st folder of outputFolder whose name starts with baseName)
		move files of aFolder to destinationFolder
	end repeat
end tell


Dude that’s amazing. You ROCK!!!

This just saved me countless hours of time. Amazing how 10 lines of code got the job done.

Thank You, Thank You, Thank You

Okay I need one more thing. I guess I was all the folders don’t match up, so I need an error loop.

Just something to say if the folder doesn’t exist in OUTPUT then skip and move on.

Thanks!!

Here is my latest and greatest, with an output bin when folders don’t exist in output folder.


set inputFolder to choose folder with prompt "Choose input folder"
set outputFolder to choose folder with prompt "Choose output folder"
set outputError to choose folder with prompt "Choose Error Bin"
tell application "Finder"
	set allFolders to folders of inputFolder
	repeat with aFolder in allFolders
		set baseName to text 1 thru 5 of (get name of aFolder)
		if exists (1st folder of outputFolder whose name starts with baseName) then
			set destinationFolder to (1st folder of outputFolder whose name starts with baseName)
			move files of aFolder to destinationFolder
		else
			move files of aFolder to outputError
		end if
	end repeat
end tell

Okay it’s not working.

The scripts keeps timing out when the folder doesn’t exist in the OUTPUT. Any ideas on how to error check it?

Still beating my head against a wall.

I need a way to error check the script.

Example:
inputFolder = 12345

Script looks for 12345 in output folder, but doesn’t find it.

Script errors out and stops working. Any ideas on how to check if a folder that starts with 12345 exists in ouputFolder?

Thanks,
Mark

If the corresponding folder in output does not exist the file(s) are moved to outputError.
What kind of error do you get?

I get an Finder Time out error and then Finder hangs at 100% CPU. Only way to fix it is to force quit finder.

edit w/exact error:
error “Finder got an error: AppleEvent timed out.” number -1712

That’s probably not the fault of the script.
A time-out occurs, when the execution of the Apple Event (in this case the copy process) takes more than 2 minutes.

If there are hugh files or a hugh amount of files it would be better to use the shell,
try first to add a timeout block


.
		with timeout of 60 * minutes seconds
			if exists (1st folder of outputFolder whose name starts with baseName) then
				set destinationFolder to (1st folder of outputFolder whose name starts with baseName)
				move files of aFolder to destinationFolder
			else
				move files of aFolder to outputError
			end if
		end timeout
.