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

Sep 7, 2017 1:43 AM in response to mrtksgl

Hi mrtksgl, I have the same problem: the script works but it creates a folder for every file!


What I need is that the files


file01_name

file02_name

file03_name

(etc)


AND


otherfile01_name

otherfile02_name

otherfile03_name

(etc)


are automatically stored in the two distinct folders:


/file01_name

/otherfile01_name


I'm trying to search around but no joy till now... can anyone help us please?

Feb 1, 2010 7:40 AM in response to BadPuppy

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>

select the folder in question in Finder and press "run" in script editor.

Jun 17, 2010 9:07 AM in response to V.K.

hi and thanks for the script,

but I have one question which ,I think, will make things more complex.

I have 15000 photos with 100+ events. Filenames are generated from the events names having sequential numbers if the photos are from the same event.

example;
event 1 0001.jpg
event 1 0002.jpg
event 1 0003.jpg
tuesday night 0001.jpg
tuesday night 0002.jpg
tuesday night 0003.jpg
monday morning 0001.jpg
monday morning 0002.jpg
monday morning 0003.jpg
and so on.

When I use this script it create folders based on every filename which makes it unusable.

Is there a way to create folders for the each sequential filename group and move into it.

Thanks for the time...

Jul 13, 2011 8:58 PM in response to V.K.

Your script works like a charm! I'd like to point out for those who, like me, don't have much script editor experience: the header and footer of VK's post are not part of the script. The part you need to copy and paste is:


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


This worked perfectly for me on a folder of .mkv files. No ".mk" extension for me.

Jul 14, 2011 7:00 AM in response to DoomedTX

Yes, It works like a charm.

Still, I have some questions how to alterate.


I have a library of movies that I unfortunately have in this way:


ABC

Adulthood.avi

Adulthood.jpg

Apocalypto.avi

Apocalypto.jpg


When I start the script it runs perfect until it comes to Adulthood.jpg and can not go on because the folder with that name already excist.


I would like to change the script so that it goes through all files with a certaing extension and moves all files with the same name (no matter what extension it has)


Does anyone know how to do this change in the script?


The best thing would be to be able to write something like this:

(file extension: .avi;mpg;mpeg;mkv etc)


/Anders

Jul 14, 2011 9:46 PM in response to a94andwi

I'm not familiar with the syntax of AppleScript, but it seems like all you have to do is figure out the command to make this happen:


move new_name.* to new_folder


The trick is figuring out how to combine the variable new_name with the ".*" to create a new variable for the move command.


Here's my first try at a script for you. It might not work, but it should point you in the right direction:


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

set wildcard_name to new_name & ".*"

move wildcard_name to new_folder

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.