Set path based on filename

Hi all,

Newbie here… Did some digging but came up empty. I want to be able to select multiple files and have each file moved to a different location. I want the location to be determined by the filename. For example: a file named Finance-smithb_mileage.doc would get saved to a folder called smithb inside of a Finance folder. So it would create the save to path based on parts of the filename. This way, if I have 10 different mileage forms from 10 different users, I can have those files automatically saved to their folder once I am done working on them. I hope this makes sense. Any help is appreciated!

Makes perfect sense.

Something like this:

set FileName to "Finance-smithb_mileage.doc"
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-"
set TI to text items of FileName
set FileType to item 1 of TI
set AppleScript's text item delimiters to "_"
set Person to first text item of item 2 of TI
set AppleScript's text item delimiters to tid
{FileType, Person} --> {"Finance", "smithb"}

Hi,

assuming that all your files have exactly the same structure xxxxxx-yyyyyy_zzzzzz.ext
try this droplet, save it as application and drop the files onto it.
The first time, or if you double click the droplet, you will asked for the base folder.
Set the property deleteOriginals to true, if you want to delete the files after being copied.

Note: the shell command ditto works like cp, but it creates silently all needed folders in the path, which don’t exist


property baseFolder : missing value
property deleteOriginals : false

on run
	set baseFolder to choose folder with prompt "Choose base folder"
end run

on open theItems
	if baseFolder is missing value then set baseFolder to choose folder with prompt "Choose base folder"
	set TID to text item delimiters
	repeat with oneItem in theItems
		set text item delimiters to ":"
		set Nm to last text item of (oneItem as text)
		set text item delimiters to "-"
		set {folder1, theRest} to text items of Nm
		set text item delimiters to "_"
		set folder2 to text item 1 of theRest
		set destination to quoted form of (POSIX path of baseFolder & folder1 & "/" & folder2 & "/" & Nm)
		do shell script "ditto " & quoted form of POSIX path of oneItem & " " & destination
		if deleteOriginals then do shell script "rm -r " & quoted form of POSIX path of oneItem
	end repeat
	set text item delimiters to TID
end open

Thank you both! One more question- sorry I’m new to this. I want to be able to double click on the application and get a list of files within a folder. I then want to be able to select multiple files and have those files saved to the network path using the script above. I have the script to create the list of files to select, and now I have the script that reads the filenames and gets the save to path according to the filename. But I am having trouble combining the scripts into one script that does both things. Below is the script to create a list to select multiple files. Can you tell me how I can combine this script with the one above so that the files that I select from the list are the ones that get saved to the network according to filename? Thanks again!

try this


property sourceFolder : "OD-IMAC:Users:admin:distribute:"
property destinationFolder : "OD-IMAC:Users:admin:Finance:"
property deleteOriginals : false

on run
	tell application "Finder" to set nameList to name of files of folder sourceFolder
	set sel to choose from list nameList with prompt "Choose file(s)" with multiple selections allowed
	if sel is false then return
	set theselection to {}
	repeat with s in sel
		set end of theselection to alias (sourceFolder & s)
	end repeat
	open theselection
end run

on open theItems
	set TID to text item delimiters
	repeat with oneItem in theItems
		set text item delimiters to ":"
		set Nm to last text item of (oneItem as text)
		set text item delimiters to "-"
		set {folder1, theRest} to text items of Nm
		set text item delimiters to "_"
		set folder2 to text item 1 of theRest
		set destination to quoted form of (POSIX path of destinationFolder & folder1 & "/" & folder2 & "/" & Nm)
		do shell script "ditto " & quoted form of POSIX path of oneItem & " " & destination
		if deleteOriginals then do shell script "rm -r " & quoted form of POSIX path of oneItem
	end repeat
	set text item delimiters to TID
	display dialog "The files have been saved."
end open

Worked perfect! Thank you very much for your help!