1

I have a list of files in a target folder:

Example/HUVEC.csv
Example/HUVEC-1.csv
Example/HUVEC-3.3.2n-1.csv (random hash)
Example/Endo1.csv

What I'd like is to be able to create a folder called "HUVEC" if there are files that start with "HUVECxxx" in the "Example" Folder and then move the files that starts with "HUVECxxx" to the "HUVEC" Folder.

Here's what I have so far, having been modified from the simple version (see below)

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

tell application "Finder"
set fileList to files of mgFilesFolder

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" and (exists folder (HUVEC_folder)) then
        move aFile to HUVEC_folder
    else
        set HUVEC_folder to make new folder in mgFilesFolder with properties {name:"HUVEC"}
    end if
end repeat

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" then move aFile to HUVEC_folder
end repeat


end tell

A more simplistic version works for one file

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

tell application "Finder"
set fileList to files of mgFilesFolder

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" and   set HUVEC_folder to make new folder in mgFilesFolder with properties {name:"HUVEC"}
    end if
end repeat

repeat with aFile in fileList
    set prefix to name of aFile
    if prefix starts with "HUVEC" then move aFile to HUVEC_folder
end repeat


end tell

but the issue with multiple files I'm quite certain is that if the folder already exists then the script fails.

Basically I think the biggest issue is how to check if a folder exists that otherwise should be created!
Thanks for any help, still relatively new at Applescript.

2 Answers 2

2

Instead of manually looping through files, you can take advantage of AppleScript's keyword every, which:

specifies every object in a container (ref.)

Here's what the script looks like when you employ this handy little feature:

    property prefix : "HUVEC" -- The prefix for file sorting

    set mgFilesFolder to choose folder with prompt "Where are the files stored?"


    tell application "Finder"
        
        -- Retrieve all files that have names
        --  beginning with the prefix
        set fileList to every file of folder mgFilesFolder ¬
            whose name starts with prefix
        
        if fileList is {} then return "No files to move." -- Nothing to do
        
        
        -- Check to see if a folder with the right
        -- name exists; if not, create it.
        if not (exists folder named prefix in folder mgFilesFolder) ¬
            then make new folder in folder mgFilesFolder ¬
            with properties {name:prefix}
        
        
        set prefix_folder to the ¬
            folder named prefix in the ¬
            folder mgFilesFolder
        
        
        -- Move the files into the folder and return
        -- a list of file references for them
        return move the fileList to the prefix_folder
        
    end tell

I've put comments throughout the code to explain what each section of the script does. But, not once did I need to use a repeat loop.

Your other issue was in trying to combine the checks for both file name and the existence of the destination folder into a single command, which got repeated over and over in the repeat loop. I kept everything separate so each line of code knows what it's doing and will do it well. If something doesn't work, it's easy to pinpoint where the problem arises:

  1. Get the relevant files.
  2. If there are none, stop the script—no need to keep going.
  3. If the destination folder doesn't exist, create it.
  4. Get the reference for the destination folder.
  5. Move the files into the destination folder.

Done.


Addendum: Bash...?

Have you thought about using bash scripting for this sort of task ? It's about ten-thousand times faster for large numbers of files, and here's an example of code that accomplishes what you want:

[[ -d HUVEC ]] && mv HUVEC*.csv HUVEC || { mkdir HUVEC && mv HUVEC*.csv HUVEC; }

😀

Sign up to request clarification or add additional context in comments.

4 Comments

Works perfectly. I should have added in the first post that I'd like to be able for all files in there, such that both "HUVEC" and "ENDO" (see example) are moved to their respective new folders "HUVEC" and "ENDO". I'll keep playing around with the code that you mentioned.
And yes, I did come across some Bash example scripts but I wasn't sure how to call bash scripts easily from the Finder...at some time point I'll have to investigate that as well!
Well… That’s partly why I created a variable called prefix at the start of the script. You can imagine that, if you change the value of the variable from HUVEC’ to ENDO`, then it would be the ENDO- files that are moved into a folder of the same name. It’s not a big leap to conceptualise how you can achieve both in one script 🙃 But, if you need a hand, let me know. Give it a try yourself first, though.
I had to place another answer to fit all of my code response...see below. and thanks for your help.
0

CJK - I've made some modifications but still there is a minor problem. The code below introduces another variable, prefix1, with my other start string "ENDO".

The script complies and runs and goes so far as to create the prefix1 folder, however the "ENDO" file itself fails to move with the two return move... commands at the end of the script.

I've confirmed that the order matters, i.e. if I switch the order of the lines

return move the fileList1 to the prefix_folder1
return move the fileList to the prefix_folder

then the ENDO files are appropriately moved but the HUVEC files fail to move.

    set prefixes to {"HUVEC", "ENDO"}

    set mgFilesFolder to choose folder with prompt "Where are the files stored?"


    tell application "Finder" to ¬
        repeat with prefix in prefixes

            set fileList to every file of folder mgFilesFolder ¬
                whose name starts with prefix

            if (count fileList) is not 0 then

                if not (exists folder named prefix in folder mgFilesFolder) ¬
                    then make new folder in folder mgFilesFolder ¬
                    with properties {name:prefix}

                set prefix_folder to the folder named prefix ¬
                    in the folder mgFilesFolder

                move the fileList to the prefix_folder

            end if
        end repeat

6 Comments

Delete the word return from both lines.
Thanks - I thought that I had tried that, but apparently not.
I edited your code block to give you the more streamlined version of your solution. Well done for solving it yourself. This new version does exactly what yours does, but without the manual labour of having to type out all the commands a second time. In fact, now you can have a thousand different prefixes and they'd all be sorted into folders by their prefix name without having to write any additional code (except for adding them to the prefixes list at the top. I'm hoping you can see the edits; it says they're being peer-reviewed (this isn't the correct way to use the answer section).
Great! That was the next thing that I was going to conceptualize, i.e. repeats were going to be necessary at some point with multiple "prefixes". Thanks much!
@CJK - any thoughts on how I might accomplish this same script in Bash? I have been playing around with it, see [stackoverflow.com/questions/49437936/… but haven't gotten it quite to work. Would appreciate any help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.