Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sort files into folders according to name?

Hello!


I have a set of static folders already created, and I am trying to sort files into them according to name. The files follow the format "Last Name, First Name.csv" and the subfolders are named "Last Name, First Name Folder" The subfolders are in a different directory from the source folder (with all the to-be-sorted files).

I've tried to Frankenstein some other apple scripts, but having trouble moving the file into the corresponding specific folder in a different directory.


on run {input, parameters}


set output to {}


repeat with anItem in the input


set anItem to anItem as text

tell application "System Events" to tell disk itemanItem

set theContainer to path of container

set {theName, theExtension} to {name, name extension}

end tell

if theExtension is in {missing value, ""} then

set theExtension to ""

else

set theExtension to "." & theExtension

end if

set theName to text 1 thru -((counttheExtension) + 1) of theName-- the name part


tell application "System Events" to set {Nm, Ex, pPath} to anItem's {name, name extension, POSIX path of container}

set BN to text 1 thru ((get offsetof "." & ExinNm) - 1) of Nm

set thePath to (pPath & "/" & BN & "/" as text) -- THIS SHOULD BE WHERE I CAN ENTER THE OTHER DIRECTORY INFO, BUT ERRORS ABOUND


do shell script "mkdir -p " & quoted form of thePath


delay 0.5

tell application "Finder" to moveaFiletoPOSIX filethePath

end repeat

return input-- or output


end run

iMac, OS X Yosemite (10.10.1)

Posted on Feb 23, 2015 4:56 PM

Reply
Question marked as Best reply

Posted on Feb 24, 2015 8:05 AM

There are some things I don't understand about what you're doing here. I've done an AppleScript only (no shell script) version of what I think you're trying to do.

When saved as an app, drag files onto it:

1. Destination is hard-coded as the Downloads folder, but you can modify this or comment it and uncomment preceding line to get the script to ask for a destination folder.

2. A list is made of all the existing folders in Destination

3. For each file:

a. If a folder with the filename exists the file is moved there

b. If the folder does not exist a new one is made and the file is moved there

c. The list of folders in Destination is updated.

There's no error checking (or distinction between files and folders dropped on the app), so proceed at your own risk with data that you have backed-up.


on openfileList


tell application "Finder"



--set theDestination to (choose folder)

set theDestination to alias ((path to home folder as text) & "Downloads")


set folderList to name of every folder of theDestination


repeat with theFile in fileList


set theInfo to info fortheFile

set fileName to name of theInfo


if fileName contains "." then

set theExt to name extension of theInfo

set extLen to (length of theExt) + 2

set folderName to text 1 thru -extLen of fileName

else

set folderName to fileName

end if


if folderName is not in folderList then

set newLocation to (makenewfolder in theDestinationwith properties {name:folderName})

else

set newLocation to folderfolderName of theDestination

end if



movetheFiletonewLocation


set folderList to name of every folder of theDestination


end repeat


end tell


end open

5 replies
Question marked as Best reply

Feb 24, 2015 8:05 AM in response to gandrew1055

There are some things I don't understand about what you're doing here. I've done an AppleScript only (no shell script) version of what I think you're trying to do.

When saved as an app, drag files onto it:

1. Destination is hard-coded as the Downloads folder, but you can modify this or comment it and uncomment preceding line to get the script to ask for a destination folder.

2. A list is made of all the existing folders in Destination

3. For each file:

a. If a folder with the filename exists the file is moved there

b. If the folder does not exist a new one is made and the file is moved there

c. The list of folders in Destination is updated.

There's no error checking (or distinction between files and folders dropped on the app), so proceed at your own risk with data that you have backed-up.


on openfileList


tell application "Finder"



--set theDestination to (choose folder)

set theDestination to alias ((path to home folder as text) & "Downloads")


set folderList to name of every folder of theDestination


repeat with theFile in fileList


set theInfo to info fortheFile

set fileName to name of theInfo


if fileName contains "." then

set theExt to name extension of theInfo

set extLen to (length of theExt) + 2

set folderName to text 1 thru -extLen of fileName

else

set folderName to fileName

end if


if folderName is not in folderList then

set newLocation to (makenewfolder in theDestinationwith properties {name:folderName})

else

set newLocation to folderfolderName of theDestination

end if



movetheFiletonewLocation


set folderList to name of every folder of theDestination


end repeat


end tell


end open

Feb 24, 2015 1:54 PM in response to gandrew1055

Hello


You may use the following shell script as a Run Shell Script aciton in your Automator workflow.


Specify the destination directory where your subfolders reside as DST in script, which is currently ~/Desktop/test folder.


E.g., A file "abc, def.csv" is moved to $DST/abc, def/abc, def.csv



Shell = /bin/bash

Pass input = as arguments

Code = as follows



#!/bin/bash DST=~/desktop/test\ folder for f in "$@" do n="${f##*/}" d="$DST/${n%.*} Folder" [[ -d "$d" ]] || mkdir -p "$d" mv "$f" "$d" done




A resulting workflow would look like this:



User uploaded file




Regards,

H

Sort files into folders according to name?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.