Mull
Mull

Reputation: 15

Applescript - move files to folder which name begins with first 2 characters of filename

I'd like a little help fine tuning this script. Is it possible to have this script match subfolders based on the first two letters of file? Parent folder would be "Shots" which contains subfolders with unique two letter prefix "BA_Bikini_Atol" which contains subfolders within those folders for the specific shots ba_0020, ba_0030, etc. Would like to move a file, ba_0020_v0002 to the ba_0020 folder by selecting "shots" as the target and the script looks through all the sub folders for a match. Thoughts?

set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")

tell application "System Events"
    set folderList to name of folders of mgDestFolder
    set fileList to name of files of mgFilesFolder
end tell

repeat with i from 1 to (count folderList)
    set folderName to item i of folderList
    set filesToMove to {}
    repeat with j from 1 to (count fileList)
        set filename to item j of fileList
        if filename begins with folderName then
            set end of filesToMove to alias ((mgFilesFolder as string) & filename)
        end if
    end repeat

    tell application "Finder"
        move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
    end tell
end repeat

Upvotes: 0

Views: 1736

Answers (2)

markhunte
markhunte

Reputation: 6932

This works in my tests

     set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
        set mgDestFolder to (choose folder with prompt "Where is the destination folder?")


        (* get the files of the mgFilesFolder folder *)
    tell application "Finder" to set fileList to files of mgFilesFolder

    (* iterate over every file *)
    repeat with i from 1 to number of items in fileList
    set this_item to item i of fileList

    set this_file_Name to name of this_item as string

    (* get the file name code
    Using the delimiter "_"  break the name into fields of text and returning fields 1 thru 2 .   i.e ba_0030 *)
    set thisFileCode to (do shell script "echo " & quoted form of this_file_Name & " |cut -d  _ -f 1-2")
    log thisFileCode
    tell application "Finder"
        set folderList to ((folders of entire contents of mgDestFolder) whose name starts with thisFileCode)
        if folderList is not {} then

            move this_item to item 1 of folderList
        end if
    end tell
end repeat

FROM:

enter image description here

TO:

enter image description here

Upvotes: 1

sirbaxx
sirbaxx

Reputation: 3

I've made (with the help from users of this site) the script you're referring to.
I've made similar (more efficient) scripts since then. Most of them depend on ASObjC Runner which you can get here: Download ASObjC Runner

If you have it installed I think the following script will work:

set mgFilesFolder to (choose folder with prompt "Where are the file stored which you would like to move?")

set mgDestFolder to (choose folder with prompt "Where are destination folders?")

tell application "ASObjC Runner"

set mgFiles to enumerate folder mgFilesFolder with recursion without including folders
repeat with mgFile in mgFiles
    set mgPrefix to text 1 thru 7 of name of (about file mgFile include only "name")
    set mgDest to enumerate folder mgDestFolder match prefix mgPrefix with recursion
    manage file mgFile moving into mgDest
end repeat
end tell

Upvotes: 0

Related Questions