Benches Uncategorized

14

Create Folder according to file names: Apple Support Communities

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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!!

14 replies

Sign in or join Beachball Help to reply.

  1. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  2. 0

    BadPuppy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks so much. I will try when I get home.

    Question, if Root already has other sub folders, will they be affected?

  3. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    BadPuppy wrote:
    Thanks so much. I will try when I get home.

    Question, if Root already has other sub folders, will they be affected?

    no, they will not be affected. the script only checks for files not folders and processes those.

  4. 0

    BadPuppy

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks man. Worked like a charm.

    Now, would you happen to know how to use automator/applescript to create a list of all the folders within Root and populate them to an excel file?

    Thanks a lot in advance.

  5. 0

    mrtksgl

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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...

  6. 0

    cliveports

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi V.K. I stumbled across this when trying to find out how to make folders from filenames and then move th efiles into those folders, which your script does perfectly! Thank you! But, if I save it as an app, select the folder I want to be populated and double click the executable nothing happens, any advice please?

  7. 0

    Dhruv1981

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Badpuppy..
    I too have been trying to develop this automator action, but with no luck. Have you found a way in Automator itself? I tried the applescript suggested to you, but nothing happens when I click a folder and then hit Run in the script editor.

  8. 0

    Nick Wild

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi,

    Did anyone get this to work in Automator - I would really like to be able to do this from a Folder Action (i.e. Hazel copies a file to a designated folder and then the folder action creates a new sub-folder and moves the file to within it)

    TIA

    Nick

  9. 0

    Mobius8

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    This worked like a charm. Placed the script in the /Library/Scripts/Folder and ran perfectly. Thanks a million.

  10. 0

    carter3d

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    This worked great for me as well, except for files that end in .mkv extensions.  For these files, the folder names generated are "whateverfilename.mk".  For example, for the file "Alien (1979).mkv", the folder name generated is "Alien (1979).mk".  Any ideas?

  11. 0

    DoomedTX

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  12. 0

    BDAqua

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Great post for clarifying things, thanks!

  13. 0

    a94andwi

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  14. 0

    DoomedTX

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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