Wednesday, September 18, 2019

#1 2019-05-21 07:34:48 pm

pukoh
Member
Registered: 2019-05-21
Posts: 2

Moving files with similar names into folders

I have a bunch of files with common first section of names that I want to put into folders that are called the same thing.

From this
j7vpsydb7i3q.jpg

To this
q2bm3iduz9e5.jpg


I'm a scripting noob, have tried to used automator, but probably a bit too complex for standard automator components.
Any help would be appreciated!

Offline

 

#2 2019-05-22 06:40:39 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 414

Re: Moving files with similar names into folders

Welcome to Macscripter.

FYI, this forum is aimed at helping people with their scripting problems, not writing scripts for people from scratch for free. ie, in general, you should be posting code you tried to create to accomplish the task and asking for help, not asking for scripts from scratch.

But it's a short script so I wrote it anyway in this case.

This assumes the consistent part of the name you want to sort by will always be delimited by the first underscore in the name, as shown in your example.

Applescript:


tell application "Finder"
   set sortFolder to choose folder
   set theFiles to every file of sortFolder
   repeat with aFile in theFiles
       set fileName to the name of aFile
       set baseName to (characters 1 through ((offset of "_" in fileName) - 1) of fileName) as text
       if not ((((POSIX path of sortFolder) & baseName) as POSIX file) exists) then make new folder at sortFolder with properties {name:baseName}
       move aFile to folder baseName of sortFolder
   end repeat
end tell

Last edited by t.spoon (2019-05-22 06:41:55 am)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2019-05-22 07:08:23 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5005

Re: Moving files with similar names into folders

Hi t.spoon.

That could be made slightly simpler and safer:

Applescript:


tell application "Finder"
   set sortFolder to choose folder
   set theFiles to every file of sortFolder
   repeat with aFile in theFiles
       set fileName to the name of aFile
       set baseName to text 1 through ((offset of "_" in fileName) - 1) of fileName -- Not 'characters … as text'.
       if not (folder baseName of sortFolder exists) then make new folder at sortFolder with properties {name:baseName}
       move aFile to folder baseName of sortFolder
   end repeat
end tell


NG

Offline

 

#4 2019-05-22 08:50:49 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 414

Re: Moving files with similar names into folders

Thanks. The "text" instead of "characters" I definitely should have known better.

The "POSIX file" coercion is because I grabbed the line from another script which looks for a much longer path of subfolders, where converting it to text and appending the long path in one go seemed preferable to me compared to "folder x of folder y of folder z."

Another sometimes advantage of the POSIX path construction is that the check to see if it exists works outside a Finder tell statement, unlike the "(folder baseName of sortFolder exists)." That's not relevant here, since we're inside a Finder tell anyway, but is useful when I'm checking to see if the correct path exists to save a file just before performing a save inside a "tell" for another application.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#5 2019-05-23 12:11:47 am

pukoh
Member
Registered: 2019-05-21
Posts: 2

Re: Moving files with similar names into folders

Thanks for your generous help!
Have always been interesting in scripting from scratch, mostly just cobble stuff together.
But I shall learn more so I can contribute to the site.

Thanks again!

Offline

 

#6 2019-05-23 05:19:03 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 893

Re: Moving files with similar names into folders

It won't make any performance difference here, however, inserting a standard additional call like choose folder into a tell block is technically a no-no, as an error is returned in the events.

Applescript:

tell application "Finder"
   set sortFolder to (choose folder)
--etc.
end tell

Standard additions should either exist outside the tell or be escaped. I generally do the latter.

Applescript:

tell application "Finder"
   set sortFolder to my (choose folder)
end tell

Offline

 

#7 2019-05-24 01:47:27 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5005

Re: Moving files with similar names into folders

Hi Marc.

While it's generally a good idea not to use scripting addition commands in application 'tell' statements (in order to avoid possible terminology clashes), user-interaction commands like the 'choose…' and 'display…' ones are exceptions. The dialogs are displayed by applications — either the one running the script or one targeted in the code — so targeting these commands at specific applications (say one known to be frontmost at the time or to be involved in what follows) can be perfectly legitimate.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)