Create new folder for each file

applescriptcommand linefile-transferfinderfolders

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

Best Answer

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