Move files to another folder starting with certian letter...

How would you use Applescript to quickly move 30,000 files from one folder to another, but moving them pieces at a time, moving files that start with the letter ‘a’ to a folder named ‘a’ then move files that start with the letter ‘b’ to a folder named ‘b’ and so on? If Applescript can’t do it, is there some other way to move them faster than opening the folder and waiting for ever, dragging them, and waiting for ever, dropping them in the folder, and then waiting for ever, 26 times?

Hi,

AppleScript can do this. This script will make folders “a” through “z” in the chosen folder and move the files to the right folder:

set the_chars to “abcdefghijklmnopqrstuvwxyz”
set the_folder to choose folder – folder with files
tell application “Finder”
activate
repeat with this_char in the_chars
set new_folder to (make new folder at the_folder with properties {name:this_char})
–ignoring application responses
move (every file of the_folder whose name begins with this_char) to new_folder
–end ignoring
end repeat
end tell

You might encounter issues with speed, so you might want to make all the folders first.

gl,

It looks like this might be too big of a job for it.

Finder got an error: The operation could not be completed because there is allready an item with that name.

In Applescript some text was highlighted…
make new folder at the_folder with properties {name:this_char}

so I got rid of that text.

Finder got an error: AppleEvent timed out.

Finder then has the beachball spinning when on the finder, until I restarted finder. It’s red in the Force Quite.

In the first try with out the folders added, it just gave the timed out error message and showed the cursed beachball when on the finder, until I restarted the finder.

The folder has 30,854 files.

Hi Nintendo,

I didn’t add error checking, but mainly wanted to show you how to ‘move’ files. If you get rid of the make folders line, then you won’t have a reference of where to move the files. I didn’t know exactly where you wanted to move the files. I didn’t know where the folders to move them to would be or if the folders were already made.I didn’t know if any files already existed in the folders. I didn’t know if any files might have the same name as a folder. Keeping all this in mind, you would add error checking for things that could happen.

Also, are you going to continue to use the script on newly added files? Then, you need to think about what to do if there is already a file with the same name in one of the folders. Say you add a new file named “NewFile”. You run a script that moves that file to folder “n” and get an error because there is already a file with that name. If rename the file, then you have to think about a naming convention. Or you could replace the old file.

Here’s a script that’s more generic and show how to move files:

Note that if the folders “a”, “b”, “c”, etc. will never be removed, then you’ll won’t need the make folders part. Added the with timeout so it shouldn’t timeout.

set the_chars to “abcdefghijklmnopqrstuvwxyz”
set the_folder to choose folder – folder with files
with timeout of 10000 seconds
tell application “Finder”
activate
try
set main_folder to (make new folder at desktop with properties {name:“Main Folder”})
on error
set main_folder to (folder “Main Folder” of desktop)
end try
repeat with this_char in the_chars
try
set sub_folder to (make new folder at main_folder with properties {name:this_char})
on error
set sub_folder to (folder (contents of this_char) of main_folder)
end try
try
move (every file of the_folder whose name begins with this_char) to sub_folder
on error – a file exists with the same name
– do something
end try
end repeat
end tell
end timeout

gl,

Just out of curiosity, could this be done with a shell script? I tried to figure it out but couldn’t. I don’t know if you can use regular expressions in a cp command, but I want to say something like

cp /…/([a-b])* /…/($1)/

But that doesn’t work, and neither do any of the variants I tried.