Move files into folder based on matching file name

Started by jinesh, January 29, 2015, 05:52:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jinesh

Hi

I have bunch of files which I want to move into folders. In these folders, there are files of same filename but different extension. The folders also have some subfolders in them.

Is there any way using Applescript I can move these files into folders if the file with same name exists in that folder?

I tried looking on google but couldn't find anything. If someone can help me that will be great.

Thanks a lot!

Joe

If the filename is the same but the extension is different a straight copy will work. It won't overwrite files with a different extension name. The extension is part of the name so in reality if the extension is different the filenames are not the same.
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

swampymarsh

In addition to Joe's comments, if you are still looking to automate and don't know AppleScript – don't forget about Apple Automator!

You can create a "folder action" watched folder and specify input such as "filter finder items (by extension)" and "copy finder items" etc.

A lot of power with a simple drag n drop interface.

I would build the "flow" first using the "application" option, then when you are happy it works create the same steps in a "folder action".

DCurry

Here's an Automator app I found a couple years ago. I didn't create it, but I used it at my last job to organize my 1-bit TIFFs for archiving. It will look at all the file names in a folder and create a new folder based on the first one it finds, then move all other files matching the name into the folder. When it comes across a name change, it makes a new folder and populates it.

I don't think it will look into subfolders, but here it is anyway.
Prinect • Signa Station • XMPie

Build a man a fire, and he'll be warm for a night. But set a man on fire, and he'll be warm for the rest of his life!

jinesh

Thanks for your replies guys.
See attached screen grab for my folder structure. There are 100s of folders that way.
http://i.imgur.com/CL4jr6f.png

I had converted the files in these folders for someone and the script which I used put all the files in one place instead of the original folder. So I don't want to go looking for a file in each folder so I can move the converted files to the same folder.

I don't think automator can do that so that is why I am looking for applescript.

Thanks again!


swampymarsh

That "auotmator" application was actually an AppleScript.

Here is the code, in case it helps and somebody can edit it:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
set {name:Nm, name extension:Ex} to info for (aFile as alias)
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set dateFolder to text 1 thru 5 of Nm
set sourceFile to quoted form of POSIX path of (aFile as text)
set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
do shell script "ditto " & sourceFile & space & destinationFile
do shell script "rm " & sourceFile
end repeat

swampymarsh

#6
You can probably find similar scripts or people to help at Mac specific scripting sites, such as:

http://macscripter.net

Please let the forum know how you go, it may help somebody else too...