Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Create Folder according to file names

Hello, I've been trying to do something "I thought" would have been very simple to do with Automator but with no success so far.

Work required: I have a single folder (let's call it Root) that contains a lot of files (note there are only files, no subfolders under Root). I want to make a workflow that will create a separate folder within Root based on each file's filename and the respective files will be moved into this folder (note just the filename, no extension.)

Example: Say I have 2 files in Root right now - ABC.zip and XYZ.avi. After I run Root through the workflow, 2 folders will be created within Root, called ABC and XYZ, containing ABC.zip and XYZ in each folder, respectively. There will be no more files within Root but subfolders.



I tried a few times but not sure how I can get the filename of each file and create a folder accordingly. Thanks for the help!!

Macbook Pro 2.8GHz, 4G ram, Mac OS X (10.6.2)

Posted on Feb 1, 2010 6:57 AM

Reply
37 replies

Jul 28, 2011 3:19 AM in response to a94andwi

This script solves your problem. It creates folders for selected unique files names only, files with different extensions but the same name ends up in the same folder.


You need to select your files in Finder and requires that files be sorted by name. Note the difference with previous script, this script works on selected files opposed to selected folder to increase flexibility.


tell application "Finder"

set current_folder to folder of front window

set x to (get the selection) as list

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

else

move this_file to new_folder

end if

end repeat

end tell


Happy folder creation 🙂

//Classe

Aug 22, 2011 2:11 PM in response to Classee

So how would I perform this function but move images with a similar name but the addition of an underscore? I have a directory of files that looks similar to this:


image.jpg

image_ss.psd

image_bn.psd

image.sn.psd

image1.jpg

image1_ss.psd

image1_bn.psd

image1.sn.psd

image2.jpg

image2_ss.psd

image2_bn.psd

image2.sn.psd

image3.jpg

image3_ss.psd

image3_bn.psd

image3_sn.psd



With the above script it's easy for me to slect the .JPG and create folders, but how do I then get the script to move all the .psd files into the newly created directories but not move the JPG?


So my end result would be:

image.jpg

image(directory with image psd files)

image1.jpg

image1 (directory with image1 psd files)

image2.jpg

image2(directory with image2 psd files)

image3.jpg

image3(directory with image3 psd files)


Any help is greatly appreciated


robert

Aug 29, 2011 4:45 AM in response to robertaf

Hi Robert


Here's a slightly different approach — it works as a droplet. Copy this to Script Editor, save as an application, and drag that application to your sidebar. Then just drag-and-drop your files onto the application's icon in your sidebar :


on open mgItems

repeat with mgItem in mgItems

tell application "Finder"

set mgName to name of mgItem

set mgExtension to name extension of mgItem

set the mgFolderPath to (the container of mgItem) as text

set text item delimiters of AppleScript to {"."}

set mgNewName to text item 1 of mgName

set text item delimiters of AppleScript to {"_"}

set mgNewName to text item 1 of mgNewName

set text item delimiters of AppleScript to ""

try

set mgFinalFolder to mgFolderPath & mgNewName & ":" as alias

on error

set mgFinalFolder to make folder at mgFolderPath with properties {name:mgNewName}

end try

if mgExtension is not "jpg" then

move mgItem to mgFinalFolder

end if

end tell

end repeat

end open


hope that helps a little


m.

Aug 29, 2011 5:12 AM in response to aussie macgrunt

Actually, I've just realised that a better variation would be to move this line :

if mgExtension is not "jpg" then


to immediately after this line :

set mgExtension to name extension of mgItem


That way, a folder will not be created if the only file with a particular name is a jpeg. That is, a folder will only be created when one is needed for non-jpegs. As such, it will probably run faster too.


hope that makes sense.


m.

Jan 12, 2012 9:27 AM in response to BadPuppy

hi guys!!


i am anees


i am new to scripting in mac. The below is the script i been looking for but when i run it in my mac it shows a syntax error: A “<” can’t go here. Kindly help me. also tell me how to run this script in my mac. i am using mac osx 10.6.8



this is easiest with applescript. paste the following into Script Editor.

<pre style="

font-family: Monaco, 'Courier New', Courier, monospace;

font-size: 10px;

margin: 0px;

padding: 5px;

border: 1px solid #000000;

width: 720px;

color: #000000;

background-color: #ADD8E6;

overflow: auto;"

title="this text can be pasted into the Script Editor">

tell application "Finder"

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

repeat with this_file in mlist

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

end repeat

end tell</pre>

Jan 12, 2012 10:39 AM in response to anees.pristine

Kindly ignore my previous reply. hai all i really worked with all the scripts yuou have given.. All were rocking... Thanks a lot!!!


I need a help from you. I want a script for my below requirement..



I have files like



1234_001.pdf

1234-001.pdf

1234-001.pdf

1234-002.pdf

1555_001.pdf

1555-001.pdf

1555-001.pdf

1555-002.pdf


i want to create folders like

1234 in that i want all these files

1234_001.pdf

1234-001.pdf

1234-001.pdf

1234-002.pdf


and

1555 similarly


that is i want to create 1 folder with name before hypen or underscore "1234" and copy all files that contains the file name 1234 irrespective of _001 or-001 or-002 or_003


Kindly see the screen shot below:


User uploaded file




Kindly hepl me in this script.... Thanks!!

May 2, 2012 5:30 AM in response to BadPuppy

I found this thread useful when creating a script to move files into folders based on their filenames. However I wanted to use only the first portion of the filenames (everything before -).


This script worked for me, in case others need to do the same thing. The main thing to do was use Text Item Delimiters, firstly to {":"} to extract the file name from the full path details, then to {"-"} (or whatever parsing character you need to use) to get the first part of that file name.


Cheers


tell application "Finder"


set current_folder to folder of front window


set x to (get the selection) as list


repeat with i from 1 to the count of x


set this_file to item i of x


if i is not 1 then


set previous_file to item (i - 1) of x


set prev_name to new_name


else


set prev_name to ""


end if


set text item delimiters of AppleScript to {":"}


set temp_name to last text item of (name of this_file as text)


set text item delimiters of AppleScript to {"-"}


set new_name to text item 1 of temp_name as text


if new_name is not equal to prev_name then


set new_folder to make new folder with properties {name:new_name} at current_folder


move this_file to new_folder


else


move this_file to new_folder


end if


end repeat


end tell

May 25, 2012 12:52 AM in response to BadPuppy

Hi,


the script helped me alot, but I also have a new problem.

I have files with extension and without, I modified the script a bit and it "works"... It cuts of the last letter of the created folder according on files without extension.

Here is my "modified" version:


tell application "Finder"

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

repeat with this_file in mlist

set cur_ext to name extension of this_file

if cur_ext is 0 then

set new_name to text of (name of this_file as text)

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

else

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

end if

end repeat

end tell

May 28, 2012 10:25 AM in response to Classee

Hi; I know this is an old post but I'm hoping you're still available for a little help?


I have multitudes of files that I need sorted and etc with same names but different extensions like:


626072043001.eps

626072043001.pdf

626072043001.jpg

626072043001.png

626072043001.tiff


So I would want a folder that is named 626072043001 and all five files to be moved into that.


But when I say multitudes I mean thousands (100,000 files each in 5 formats) 😟 I have them separated out somewhat into smaller groups to make them a little easier to work with, but is there any way to get your script (copied below) that will create subfolders based on filename to work on every file in a selected folder rather than making me select the files?


tell application "Finder"

set current_folder to folder of front window

set x to (get the selection) as list

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

else

move this_file to new_folder

end if

end repeat

end tell

May 28, 2012 10:37 AM in response to Fanaile

Okay, well, you can ignore me; I figured it out just a couple minutes after I posted my question.


Should anyone else ever also need to work off a folder like this, here is what worked for me:


tell application "Finder"

set selected to selection

set current_folder to item 1 of selected

set mlist to every file of current_folder

set x to mlist

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to make new folder with properties {name:new_name} at current_folder

move this_file to new_folder

else

move this_file to new_folder

end if

end repeat

end tell

Create Folder according to file names

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.