Skip navigation

Create Folder according to file names

17272 Views 32 Replies Latest reply: Jul 29, 2013 9:12 AM by markmc78 RSS
  • Classee Level 1 Level 1 (0 points)
    Currently Being Moderated
    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

  • robertaf Calculating status...
    Currently Being Moderated
    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

  • aussie macgrunt Calculating status...
    Currently Being Moderated
    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.

  • aussie macgrunt Level 1 Level 1 (0 points)
    Currently Being Moderated
    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.

  • anees.pristine Level 1 Level 1 (0 points)
    Currently Being Moderated
    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>

  • BDAqua Level 10 Level 10 (114,790 points)
    Currently Being Moderated
    Jan 12, 2012 10:29 AM (in response to anees.pristine)

    Hi Anees,

     

    Your likely to get more help on this by starting a new question, rather than being at the end of a long thread.

  • anees.pristine Level 1 Level 1 (0 points)
    Currently Being Moderated
    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:

     

     

     

     

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

  • Carpe_Diem_Rj Calculating status...
    Currently Being Moderated
    Jan 21, 2012 10:28 AM (in response to anees.pristine)

    Hello to everyone...im waiting the answer  for anees.pristine question because i have the same problem please help us!!!

  • anees.pristine Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 23, 2012 8:23 AM (in response to BDAqua)

    hi aqua!!

    thats a good thing to do

     

     

    i posted it as a new question and fane_j gave me the answer and it works

     

    thanks dude!!

  • anees.pristine Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 23, 2012 8:24 AM (in response to Carpe_Diem_Rj)

    hai carpe

     

    i posted it as a new question and fane_j gave me the answer and it works try it...

  • Carpe_Diem_Rj Level 1 Level 1 (5 points)
    Currently Being Moderated
    Jan 23, 2012 9:37 AM (in response to anees.pristine)

    Hello my friend where i can find the answer? Could you post the link?

  • Duckers Calculating status...
    Currently Being Moderated
    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

  • vanez Calculating status...
    Currently Being Moderated
    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

  • Fanaile Calculating status...
    Currently Being Moderated
    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

  • Fanaile Level 1 Level 1 (0 points)
    Currently Being Moderated
    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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (2)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.