Sparky_47
Sparky_47

Reputation: 181

Applescript to Create Folder and Move Files if Files Exists

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.

Upvotes: 1

Views: 1122

Answers (2)

Sparky_47
Sparky_47

Reputation: 181

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

Upvotes: 0

CJK
CJK

Reputation: 6092

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; }

😀

Upvotes: 2

Related Questions