REQ: Script that creates new subfolders named for files in a folder

If this message would be better posted elsewhere, please forgive the error and kindly direct me to where it would be better posted.

I need to find a script or series of scripts that will let me choose a folder which contains several files of various types (images, text, video), scan the filenames, then create folders for files that have the same first 5-10 characters in common. If I could choose the parameter of how many characters alike the script would select, that would be optimal, but it could be too difficult to do that. I need the script to then move the files with the similar filenames into the folders that it creates. I might have to run the resulting folders through another script to further refine the groupings.

I left a similar request years before when I had a PPC running Panther and received a few replies that included some great attempts, but nothing that really did the job. Now, even the “best try” of the responses I got does not work with Leopard, which puzzles me.

I’m a teacher of English and don’t have the kind of brain that can create scripts myself, but I am certain I can find a way to show my appreciation for whoever can help save me the time that this script would save. I can proofread papers or do some writing for a project on which you’re working. There’s always a way to pay someone back in kind.

Model: MacBook Pro
AppleScript: 1.1
Browser: Firefox 2.0.0.16
Operating System: Mac OS X (10.5)

Hi,

try this


property deleteOriginals : true

set processFolder to choose folder with prompt "Choose source folder"
set baseFolder to choose folder with prompt "Choose destination base folder"
set maxChar to text returned of (display dialog "Enter number of characters" default answer "") as integer

tell application "Finder" to set theFiles to files of processFolder
repeat with oneFile in theFiles
	set F to contents of oneFile as alias
	set {name:Nm} to info for F
	set folderName to text 1 thru maxChar of Nm
	set destination to quoted form of (POSIX path of baseFolder & folderName & "/" & Nm)
	do shell script "/usr/bin/ditto " & quoted form of POSIX path of F & " " & destination
	if deleteOriginals then do shell script "/bin/rm -r " & quoted form of POSIX path of F
end repeat

Hi Scoey,

I wrote many of such scripts for my former employer, where I had to manage and organize large patent databases with thousands of PDF files about electroplating. And so I just could not resist the challenge to write for one for you. Moreover I have a lot of sympathy for teachers, as I once studied to become a teacher for primary school :wink:

You can download the script file right here. Once you have downloaded it and placed it onto your Desktop, you can execute the following AppleScript code from the Script Editor:

But please use a dummy folder first!


property mytitle : "Movy"

on run
	try
		set folderpath to (choose folder with prompt "Please choose a folder to process" without invisibles and multiple selections allowed)
		set folderpath to quoted form of (POSIX path of folderpath)
		set charwidth to my askforcharwidth()
		if charwidth is not missing value then
			set pyscriptpath to quoted form of (POSIX path of (((path to desktop) as Unicode text) & "movefiles.py"))
			set command to "/usr/bin/python " & pyscriptpath & " " & folderpath & " " & charwidth
			do shell script command
		end if
	on error errmsg number errnum
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end run

on askforcharwidth()
	try
		tell me
			activate
			display dialog "Please only process the first . characters of the file names:" default answer "5" buttons {"Cancel", "Enter"} default button 2 with title mytitle
			set dlgresult to result
		end tell
		set usrinput to text returned of dlgresult
		if usrinput is "" then
			my askforcharwidth()
		else
			try
				set charwidth to usrinput as integer
			on error
				my askforcharwidth()
			end try
			if charwidth is 0 then
				my askforcharwidth()
			else
				return charwidth
			end if
		end if
	on error
		return missing value
	end try
end askforcharwidth

on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 20 with title mytitle
	end tell
end dsperrmsg

If this works for you, we can further customize it for you.

Here’s another solution. It creates subfolders in the chosen folder. If the subfolder already exists, it moves the appropriate files into it.


set processFolder to choose folder with prompt "Choose source folder"
set maxChar to text returned of (display dialog "Enter number of characters" default answer "") as integer

tell application "Finder"
	set theFiles to files of processFolder
	set x to count of theFiles
	set i to 1
	repeat while x > i
		set thefile to item i of theFiles
		set thename to the name of thefile
		set chrs to text 1 thru maxChar of thename as text
		set filestomove to files of processFolder whose name begins with chrs
		if (count of filestomove) > 1 then
			try
				set destfolder to (make new folder at processFolder with properties {name:chrs}) as alias
			on error
				set destfolder to ((processFolder as text) & chrs) as alias
			end try
			move filestomove to destfolder
		else
			set i to i + 1
		end if
		set theFiles to files of processFolder
		set x to count of theFiles
	end repeat
end tell