11232

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

Question:

I've got 2 folders

the first folder contains all the files which I'd like to move to a subfolder of the second folder. I'd like to this based on the first 7 characters of both the file & folder names.

So if the first 7 characters of the subfolder in FOLDER 2 matches the first 7 characters of the file in FOLDER 1. The file get's moved into the subfolder of FOLDER 2

Right now I've got an Applescript which works, but it's terribly slow.

Here's the script so far:

set mgFileList to "" set mgDestFolder to "" 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?") set folderList to contents of mgDestFolder tell application "Finder" to set fileList to files of mgFilesFolder repeat with aFile in fileList set prefix to getPrefix(name of aFile) tell application "Finder" try set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix) move aFile to destinationFolder end try end tell end repeat on getPrefix(aName) set prefix to text 1 thru 7 of aName return prefix end getPrefix

I've only recently got into Applescripting. Can this be done more efficiently within Applescript? I've searched around for a solution which works with a shell script (which I think will probably be a lot faster), but haven't been able to get any of them working.

I'm working on OSX Mountain Lion 10.8.4

-- edit

I see that I've uploaded the wrong version of the script I've managed to put together so far. Above is the script that is working, but it's really slow. Probably because it doesn't use any shell script.

---------------update-------------------

Here is the final script that is working pretty fast right now:

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 fileList to files of mgFilesFolder whose name does not start with "." repeat with aFile in fileList set prefix to my getPrefix(name of aFile) try set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix) move aFile to POSIX path of destinationFolder end try end repeat end tell on getPrefix(aName) try set prefix to text 1 thru 7 of aName return prefix on error return aName end try end getPrefix

Answer1:

The Finder is generally a slow program because it does a lot of things on your computer. Therefore you should avoid using it if possible. In your case these tasks can be done using System Events. Try this, maybe it will be faster. Note that you had a couple mistakes in your code which are fixed in this code.

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 fileList to files of mgFilesFolder whose name does not start with "." repeat with aFile in fileList set prefix to my getPrefix(name of aFile) try set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix) move aFile to POSIX path of destinationFolder end try end repeat end tell on getPrefix(aName) try set prefix to text 1 thru 7 of aName return prefix on error return aName end try end getPrefix

Answer2:

Not sure how many files you're working with, but I tested this with 10 folders and ~100 files, and it took less than 4 seconds to run. Working with strings is much faster than working with files, so the script gets the files/folders as strings and builds aliases when they're needed.

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?") --Always use System Events whenever possible. It's WAY faster than Finder. 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 --Can't use system events for moving files. You have to use Finder. tell application "Finder" move filesToMove to alias ((mgDestFolder as string) & folderName & ":") end tell end repeat

Answer3:

It might be easier to use shell scripting:

cd FOLDER1; for f in *; do d=../FOLDER2/"${f:0:7}"*; [[ -d $d ]] && mv "$f" "$d"; done

Recommend

  • Using JavaScript files in NativeScript
  • How to copy a folder from one one path and paste it to another path using C#?
  • What is the good db property, for an keys's array for python with App Engine?
  • Copy emails from source folder if not existing in destination folder
  • Circular dependency with cross-cutting concern
  • Serializing when class inherits list (of object)
  • insert one column data from a table into other table, but the other column data will be specified dy
  • Volley, json and php connection
  • How to create a project sheet via the python SDK
  • How to mock $scope.variables in jasmine
  • QThread.isFinished returns False in slot connected to finished() signal
  • get all inner brackets/parentheses
  • Export Certificate with private key including all certificates in path using powershell
  • Applescript to show Apple menu bar items
  • non-static method encode(byte[]) cannot be referenced from a static context [closed]
  • How to read a file in multiple chunks until EOF (C++)
  • How to store values of a particular line of a text file in separate variables in Python
  • Duplicate file descriptor after popen
  • Finding the intersection between two list of string candidates
  • how to read wpa_supplicant.conf file
  • Is a conditional response possible with netcat
  • Split string in column and add value in column
  • How to handle this simple IO exception in Haskell
  • Any reason why an std::ofstream object won't close properly?
  • Applescript to open an application in full-screen mode?
  • TASM Print characters of string
  • Applescript Excel 2016 save as PDF
  • WPF DispatcherFrame magic - how and why this works?
  • How to combine multiple csv into one file in serial manner using python?
  • Building an array of files in a directory
  • Restoring Mysql dump from java: why does it hang the process?
  • Explicit instantiation declaration with extern
  • How to check a string does not start with a number in Batch?
  • HSQL + Hibernate Exception: Wrong column type: Found: double, expected: float
  • How to make R's read_csv2() recognise the text characters properly
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • C: Incompatible pointer type initializing
  • What is the “return” in scheme?
  • Compare two NSDates in iPhone