1

I have been trying to create some kind of script to automate the move of around 408 items sizing about 1.5 TB.

What I am wanting to do is create a folder based on the filename, but exclude the extension, then move the original item with the same name into the folder. All files have different extensions, and also have periods throughout the name. It need to run as either a shell script or AppleScript, as it will be running under launchd after the initial move is done for automated moves of files. Is there a good way to go about this? (Even not using AppleScript). I don’t mind using the shell or any other method that is suggested.

I found this post on StackOverflow, but could not make too much sense of it, as it seemed to just generate errors. Any help is appreciated. The files have alphanumeric sequences and random characters in the names (I think this is what is catching in AppleScript.

Edit: Sorry for the wait... was at work.

Have something like:

Movies
    |- Anchorman.mkv
    |- Big Buck Bunny.mov
    |- Spy.mp4
    |- Shawshank Redemption.avi

And I would like the result:

Movies 
   |- Anchorman *"This is a folder"*
       |- Anchorman.mkv *"This is the file"*
   |- Big Buck Bunny
       |- Big Buck Bunny.mov
   |- Spy
       |- Spy.ext
   |- Shawshank Redemption [1994](1080p)
       |- Shawshank Redemption [1994](1080p).avi
  • 2
    Could you provide more relevant and actual details? Are these "408 items" all in the same folder? Are these "408 items" all files with extensions? Are the folders to be created in the same folder the "408 items" exist? Does it have to be written AppleScript? Details, Details, Details!!! – user3439894 Aug 18 '17 at 14:28
  • If all the files are in the same directory, then in Terminal, cd to the target directory and use this simple compound command: for f in *.*; do mkdir "${f%.*}"; mv "$f" "${f%.*}"; done – user3439894 Aug 18 '17 at 23:21
3

I'll assume that all 408 are in the same folder and it have extension. Something like this:

Folder
|- File1.ext
|- File2.ext
|- File3.ext
|- File4.ext

And you want the following result:

Folder 
|- File1 *"This is a folder"*
   |- File1.ext *"This is the file"*
|- File2
   |- File2.ext
|- File3
   |- File3.ext<br>
|- File4<br>
   |- File4.ext<br>

If that's the case you can do it with the terminal with the following command (execute it inside the folder where the files are stored. It will fail creating the folder for the files without extension):

for FILE in *; do FOLDER=$(echo $FILE | sed 's/\.[A-Za-z0-9]*$//'); mkdir "$FOLDER"; mv "$FILE" "$FOLDER" ; done
  • 1
    I don't know who downvoted it, but I would have do this in bash and not Applescript as well. It's much, much more efficient. – Allan Aug 18 '17 at 23:57
  • @Allan, Unfortunately the OP is lacking in the real relevant details however, it may have been down-voted because as coded it will fail on any file that has an extension with other then alpha characters in it and or if the filename has spaces. – user3439894 Aug 19 '17 at 0:07
  • Yes, totally true. It will fail with some kind of files. Let's wait until @bret7600 add more info. – user161254 Aug 19 '17 at 0:13
  • This also can be done much more efficiently without using FOLDER=$(echo $FILE | sed 's/\.[A-Za-z]*$//'), by using parameter expansion. See my comment to the OP. – user3439894 Aug 19 '17 at 0:13
  • I don't know anything about parameter expansion. Can you explain it to me? – user161254 Aug 19 '17 at 0:18
1

As others have responded, it's hard to know exactly what you're looking for without the details. This AppleScript should be a good starting point, and you can tweak it to your needs. It will prompt you to identify a folder that holds the files you want to put into subfolders. I've put a property at top called removePeriods, which you can set to true or false (no quotes). If that's true, it will remove all periods from the file name and use that name to create a folder. Of course, you can tweak that to be any punctuation you would like. You could further have it strip other punctuation as well. Finally, it takes the file and moves it into the new folder.

property rootFolder : ""
property removePeriods : true

--Assuming all the files you want to move are in one folder.
if rootFolder is "" then
    set rootFolder to choose folder
end if

--Get a list of every visible file in the folder (assuming you don't want to process hidden files)
tell application "System Events" to set myFiles to ¬
every file in rootFolder whose visible is true

repeat with aFile in myFiles
    --Get the file name
    set aName to name of aFile
    --If removePeriods property above is set to true, take all the periods out of the file name
    if removePeriods then
        set tid to text item delimiters
        set text item delimiters to "."
        if (count of text items of aName) is greater than 2 then
            set aName to text items 1 through ((count of text items of aName) - 1) of aName
        else
            set aName to text item 1 of aName
        end if
        set text item delimiters to tid
        set aName to every item of aName as text
    end if

--Make a new folder with the name of the file, inside the rootFolder (the original folder you selected
tell application "Finder"
    set newFolder to make new folder at rootFolder with properties {name:aName}
    move (aFile as alias) to newFolder
end tell

end repeat

If you have folder within folders, that's another level but can be done with another loop. If you want all the new folders with files in them to go to a new place, you'd just need to add that path. If you want to strip only the name before the first punctuation mark to make the folder, you could set aName to the first text item only. You'd probably also want to add a check to make sure there isn't already a folder by the name that you are creating.

EDIT: At the request of another user, I'm adding a note to clarify why I set rootFolder to a property rather than a standard variable. In the photo above, it appears that all the OP's files are in the movies folder. A property, in AppleScript (and in simple terms), is a variable that is saved (stored) between runs. It will be saved until the script is compiled again, at which time it will be set back to "". A normal variable will have no memory of a previous run of the script. I chose to make rootFolder a property so that the user wouldn't have to manually select the folder to process on each run, just the first run. However, you could replace the beginning of the script through the 'end if' with the following two lines of code:

set rootFolder to choose folder
set removePeriods to true

You could also hardcode the path to the folder rather than have the user select it. There are many options. I should also say again that this is not a finished script intended for final use, I created it as a place to start. However, I have used this foundation as a part of a much larger, more complicated script, to reorganize upwards of 500,000 files grouped into uniquely named folders, so I'm confident in the structure.

  • 1. If one copies and pastes the code, as written, into Script Editor, it fails to compile! 2. Because rootFolder is a property, one is not prompted the next time run because the value of a property is not reset unless recompiled. There is no good reason for it to be a property, it should be a normal variable. Same applies to removePeriods. – user3439894 Aug 23 '17 at 0:15
  • Thanks for pointing out that it wouldn't compile. The site added a hard line break when I copied and pasted the tested code. I have edited it to be a soft line break, and now it will compile when copied from above. As for rootFolder being a standard variable, I disagree. What if the user doesn't want to have to tell it which folder to process every time. I set it as a property, because in the OP's question, it appears he is going to use this on the same folder each time. Prompting the user in that case is unnecessary. Feel free to tweak for your own use. – Hurston Aug 23 '17 at 14:44
  • 1
    As to "because in the OP's question, it appears he is going to use this on the same folder each time", that may to may not be true however the OP was sparse on details until it was edited and even then IMO there could have been more details added. I'd suggest you modify your answer to explain what's happening by setting it as a property vs. variable because users that are not that familiar with AppleScript are going to be wondering why the choose folder dialog doesn't appear again, unless recompiled. – user3439894 Aug 23 '17 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .