Sorting files, making folders

Hi there.

I’m kinda new to scripting - but I’m learning eagerly :slight_smile:

I need a little push in the rigth direction for my next project…

Here’s the deal, I have a lot (about 700) images with names like:

A12345-06
A12345-08
A12345-12
B12345-06
B12345-07
B12345-08
C12345-06

The last two digits in the filename refers to a pagenumber.

Now here’s the tricky part (well, tricky for a newbie like myself). I wan’t
my script to make a folder called “Page 01” and put all the files whose name
ends with 01 in there.

Sounds simple, huh? Well, not for me. Any help, script-snippets, pointers to
websites etc., will be extremely appriciated.

Kind regards,
Steen Villumsen

First, what OS are you using? 9 or X?

Second, post the code you’ve written so far so we can not only help you get what you want, but show you why what you have isn’t working. It’s the best way to learn.

I’m on X (10.2.5)

Here’s the code I’m working with:


set sourceDir to "Steen Villumsen:uge14"
repeat with pageNum from 1 to 99
	set pageStr to pageNum as string
	if pageNum < 10 then set pageStr to "0" & pageStr
	tell application "Finder"
		set filesToMove to (every file of folder sourceDir whose name ends with pageStr)
		if (count of filesToMove) > 0 then
			set targetDir to make new folder at sourceDir with properties {name:"Page " & pageStr}
			move filesToMove to targetDir
		end if
	end tell
end repeat

The funny thing is, when I try the script, absolutly nothing happens…

It appears that the script runs (play button in ScriptEditor turns green),
and I don’t get any error-messages.

Regards,
Steen Villumsen

Got it working - extremely cool!

But, when I showed it to my collegues who are going to be using it, some new
issues emerged:

The file-names include file-suffixes, so their name is something like
“A032202.JPG” instead. Furthermore sometimes I have more than a houndred
pages, and the filename will for instance be “A0322102.JPG” if the page is
102.

So I guess my script needs to extract the characters using a code like
“characters -7 through -5” and then know if the result is “02.” it’s suppose
to throw away the period.

Is this at all possible?

Once again, thanks to everybody for their help (and patience with a newbie),
kind regards,

Steen

Here is a folder action I use for a very similar process as yours.
Credit all goes to Bill Briggs @ MacCentral.com

on adding folder items to theFolder after receiving theFiles
	tell application "Finder"
		if (count of folders in theFolder) > 0 then
			set theSubFolders to the name of every folder ¬
				of theFolder as list
		else
			set theSubFolders to {}
		end if
		repeat until (count of files of theFolder) is 0
			set remainingFiles to every file of theFolder
			set thePath to name of item 1 of remainingFiles
			copy text (((length of thePath) + 2) - ((offset of ":" in (reverse of characters of thePath) as string))) thru (length of thePath) of thePath to fileName
			copy text 1 thru ((offset of "." in fileName) - 1) of fileName to theProduct
			if theSubFolders does not contain theProduct then
				make new folder at theFolder ¬
					with properties {name:theProduct}
			end if
			move (every file of theFolder whose name ¬
				begins with theProduct) ¬
				to folder ((theFolder as text) & (theProduct as text))
		end repeat
	end tell
end adding folder items to

When you copy text 1 thru the offset of the .jpg minus how many characters back in the name to determine the folder name. Play with this to get the right naming sequence.

Hey Brandon,

thanks for the script.
I just can’t get it working.

I’ve attached the script to a folder (using Folder Actions), and tried dumping files in the folder. I’ve also tried dumping folders in the folder - same (lack of) result.
Do you have any idea on what I’m doing wrong?

Also I was working my way through Bill Briggs articles, but now it looks like Maccentral have removed the articles… do you know of any place I can find 'em?

Thank you for all your help,
regards,

Steen

Yes, though working with file names can be tedious, as you must account for all variations in the name. I’ll help, but I’m not sure what it is you need now.

Yes, this is exactly right. Is you code for this not working?

I think I need to make my script into an app, instead of a folder action - for testing purposes I find that easier…

He, I don’t have any code yet - frankly I don’t know where to start…

All the help I can get will be greatly appriciated - thanks.

Regards,
Steen

A very good idea.

Because of the improperly-numbered files, we’ll have to make some assumptions that if wrong will truly mess things up.
So: test all scripts on COPIES of the files. Do not alter the originals until the script works properly
If it’s possible, get the creators of the files to number them properly with leading zeros.

This is to return the folder name of the destination folder.
Test it with several names to confirm it works


property full_stop : "."
property new_folder_name : "Page "

set x to build_page_name of me from "A032202.JPG"
display dialog x

set x to build_page_name of me from "A0322102.JPG"
display dialog x

to build_page_name from this_name
	-- takes files names of length and form "A032202.JPG" (11 charactes) or "A0322102.JPG" (12 characters)
	-- and returns the folder to where the file should be moved
	set page_id to text 6 thru 8 of this_name
	if last character of page_id is full_stop then set page_id to "0" & text 1 thru 2 of page_id
	return new_folder_name & page_id
end build_page_name



Does the folder containing the files to be moved contain other files?
Will all the files in the target folder be moved? (same question, worded differently)
If so, no code will be necessary to screen out the other files.

Now here’s how to loop over files in a folder:

You’ll need to change the “Documents:inprogress:” to the path to the source folder on your computer.
You’ll need to change the "Documents:miscellany: to the path to the destination folder on your computer.

Those are the paths I used for testing on my computer.
When I add the part to create the “page xxx” folder if it doesn’t exist, they will appear as subfolders of the destination_folder


property source_folder : alias ((path to current user folder as text) & "Documents:inprogress:")
property destination_folder : alias ((path to current user folder as text) & "Documents:miscellany:")


tell application "Finder" to set file_list to every file of source_folder
repeat with current_file in file_list
	tell application "Finder" to set current_name to name of (contents of current_file)
	display dialog current_name -- for debug purposes only; comment out to hide
	set temporary_folder to build_page_name of me from current_name
	display dialog temporary_folder  -- for debug purposes only; comment out to hide
end repeat

And here’s most of it put together:


property source_folder : alias ((path to current user folder as text) & "Documents:inprogress:")
property destination_folder : alias ((path to current user folder as text) & "Documents:miscellany:")
property full_stop : "."
property new_folder_name : "Page "


tell application "Finder" to set file_list to every file of source_folder
repeat with current_file in file_list
	tell application "Finder" to set current_name to name of (contents of current_file)
	display dialog current_name -- for debug purposes only; comment out to hide
	set temporary_folder to build_page_name of me from current_name
	display dialog temporary_folder -- for debug purposes only; comment out to hide
	set folder_awaits to confirm_or_deny of me from ((destination_folder as text) & temporary_folder)
	if folder_awaits then tell application "Finder" to move (contents of current_file) to alias ((destination_folder as text) & temporary_folder)
end repeat


to build_page_name from this_name
	-- takes files names of length and form "A032202.JPG" (11 characters) or "A0322102.JPG" (12 characters)
	-- and returns the folder to where the file should be moved
	set page_id to text 6 thru 8 of this_name
	if last character of page_id is full_stop then set page_id to "0" & text 1 thru 2 of page_id
	return new_folder_name & page_id
end build_page_name

to confirm_or_deny from this_path
	create_folder_heirarchy of me at this_path for ""
	try
		set folder_exists to (info for alias this_path)'s folder
	on error
		set folder_exists to false
	end try
	return folder_exists -- if the folder at this_path exists or was successfully created, returns true; otherwise false
end confirm_or_deny

to create_folder_heirarchy at this_path for next_folder
	set {old_delimiters, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
	try
		set folder_exists to (info for alias this_path)'s folder -- used to create error condition
		if (next_folder is not "") then -- if there is a subfolder, create it
			try
				tell application "Finder" to make new folder at alias this_path with properties {name:next_folder}
			on error errmsg
				display dialog errmsg -- unable to create subfolder 
			end try
		end if
	on error -- current path doesn't exist
		set current_folder to last text item of (text items of this_path) -- name of last folder
		set shorter_path to (items 1 thru -2 of (text items of this_path)) as text -- rest of path
		create_folder_heirarchy of me at shorter_path for current_folder -- call self to create last folder
		if (next_folder is not "") then -- create last folder in path
			tell application "Finder" to set x to make new folder at this_path with properties {name:next_folder}
		end if
	end try
	set AppleScript's text item delimiters to old_delimiters
end create_folder_heirarchy

Hello Guy,

wow - truely amazing. The script works like a charm! :smiley:
I can still be amazed about how people are willing to help perfect strangers, without thinking “what’s in it for me” - if only that was every mans philosophy.

Thank you so much for your help, if you’re ever in Denmark there’s Pint of your favourite brew awating you :lol:

I have two things to do now:

  1. Fiddle a bit more with the script. It has come to my attention that the filename convention is like "A12345-08.JPG instead - got that fixed already. But files can also be called “G19889-03-CLIP.JPG” and “AA19889-08.JPG”. I’m thinking about making my script look for the first hyphen (-) in the file name, and the concentrate on the next three characters. Sound sensible?
    Well, I think I’ve decided to crack this nut myself.

  2. I’m gonna take a very close look at the script you’ve wrote, to make sure I understand how it works. Hey, maby next time I’ll be able to do something like this myself.

Once again, thank you very much for your help.
The kindest regards,
Steen

Working with a variety of file names is hard. If you need more help, I’ll be here.

General Solution: Set the applescript text item delimiters to hyphen, then assign the text items to a list, then inspect each item of the list for the numbered part of the file name that will be used for the folder name.

Thank you very much. I love a good beer.

Guy

Hello Guy

I just saw your script for steen here and i thought, thats what i need for my problem. After take a look at the script i need help.

My files are caled:
13_lq_06_2003-05-22.pdf
17_so_05_2003-05-02.pdf
18_sogr_09_2003-05-12.pdf
04_bt_12_2003-05-05.pdf
page_publication_ressort_date.pdf

every day i have about 80 to 100 files in one folder on a servervolume. then i have to sort them manualy. When i saw your script i thougt thats it.

so thats what i want:
move the files from publication “so” and “sogr” to "SOC_Archiv (Servervolume) → PDF_Archiv → SOGR → 1SEM2003 → MAI → 22 or what the month or day may be

the files from publication “lq” to "SOC_Archiv → PDF_Archiv → LQ → 1SEM2003 → MAI → 22

and the files from publication “bt” to "SOC_Archiv → PDF_Archiv → BT → 1SEM2003 → MAI → 22

So you see i have to do multiple choices from one filename to put it to the right place. i need the publication, the hole date for the semester and the month and the day for the dayfolder.

the page and the ressort are not important.

So my question is this possible and can you help me on this.

thanks for any help
Roland

Yes, but as I don’t have a server, the applescript for remotely sorting files is beyond my ability to construct and test. Best to ask others at MacScripter about how to work with the server.

Instead of applescript, you might want to consider a shell script using your favorite shell: tcsh, ksh, bash, etc. A shell script will execute faster than an applescript.

mgh


Let us do one step at a time.

The most difficult part of moving files into folders is confirming that the file names are understood correctly.

From your samples, I see that the second ‘part’ of each file name guides where the file is to be stored, and the last part provides the path to the subdirectories.

" 13_lq_06_2003-05-22.pdf " → PDF_Archiv → LQ → 1SEM2003 → MAI → 22
“17_so_05_2003-05-02.pdf” → PDF_Archiv → SOGR → 1SEM2003 → MAI → 22

and so on. Breaking the file name into useful parts is easy (but tedious) provided that the file names are structured.

For the script below please make sure I have understood what you wanted correctly. (For example, I understood that the ‘22’ in ‘MAI → 22’ is to be the file name, if it isn’t, the script must be changed.) I also, alas, am unfamiliar with your native tongue (German/Deutsch?), so there may be unicode problems that will take some research to solve.

Try several file names to make sure the proper paths are generated.


property soc_archiv_path : "SOC_Archiv:PDF_Archiv:" -- highest level directory for file sort
property mystery_text : "1SEM" -- I don't know the purpose of these characters, so you may need to alter this 
-- file names are structured "13_lq_06_2003-05-22.pdf"
-- underscore separates major parts
-- dash separates date
-- period separates extension
-- I prefer to name these instead of hard-coding the dash, period, and underscore characters
property fn_separator : "_"
property date_separator : "-"
property extension_separator : "."
property directory_separator : ":"
-- applescript is not a complete programming language and so lacks features
-- it has no command to change case;  below will make up for this
property uppercase : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property lowercase : "abcdefghijklmnopqrstuwvxyz"
-- I don't think there is a simple way to change month numbers to month text
-- I am not that familiar with date functions in applescript
-- and it's possible the date functions return only English names of months, which you may not want
property all_months : {"JAN", "FEB", "MAR", "APR", "MAI", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"} -- change to proper abbreviations

set current_file_name to "17_so_05_2003-05-02"
-- set current_file_name to "13_lq_06_2003-05-22"
-- example:  "13_lq_06_2003-05-22"
-- I know the ".pdf" is missing
-- Later I will add code to ask for the names of files that omits the ".pdf" automatically
set old_delimiters to AppleScript's text item delimiters -- store current text delimiters
set AppleScript's text item delimiters to fn_separator -- set text delimiters to what we want
set file_name_parts to every text item of current_file_name
-- file_name_parts = {"13", "lq", "06", "2003-05-22"}
-- item 2 is "lq", "so", "sogr", "bt", etc.
-- last item is the date, for example: "2003-05-22"
set AppleScript's text item delimiters to old_delimiters -- restore text delimiters
set altered_path to my correct_abbreviation(item 2 of file_name_parts)
set new_destination to make_new_path at altered_path for last item of file_name_parts

display dialog new_destination

to correct_abbreviation(this_abbreviation)
	-- this subroutine maps 'so' onto 'sogr'
	-- similar corrections should be added here
	if this_abbreviation is "so" then
		set altered_text to "sogr"
	else
		set altered_text to this_abbreviation
	end if
	return altered_text
end correct_abbreviation

to make_new_path at this_directory for this_date
	-- uses global properties soc_archiv_path, mystery_text
	-- subroutines use global properties date_separator, uppercase, lowercase, all_months
	-- display dialog this_directory -- for debug purposes
	-- display dialog this_date -- for debug purposes
	set temp_path to my make_upper_case(this_directory)
	set temp_path to soc_archiv_path & temp_path & directory_separator & mystery_text
	-- display dialog temp_path -- for debug purposes
	set temp_path to temp_path & my add_date_paths(this_date)
	return temp_path
end make_new_path

to add_date_paths(this_date)
	-- subroutines use global properties date_separator, all_months, directory_separator
	-- adds the year to the current directory, then a month directory, then uses the date as the file name
	set old_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to date_separator
	set date_parts to every text item of this_date
	set temp_path to item 1 of date_parts & directory_separator
	set month_name to item (item 2 of date_parts) of all_months
	set temp_path to temp_path & month_name & directory_separator
	set temp_path to temp_path & (item 3 of date_parts)
	set AppleScript's text item delimiters to old_delimiters
	return temp_path
end add_date_paths

to make_upper_case(this_text)
	-- use global properties date_separator, uppercase, lowercase
	set changed_case to ""
	set each_char to every character of this_text
	set uc_chars to every character of uppercase
	repeat with one_char in each_char
		set changed_case to changed_case & item (offset of (contents of one_char) in lowercase) of uc_chars
	end repeat
	return changed_case
end make_upper_case

Hi Guy

Thank you very much for your fast reply.

Sorry for not explaining it right. The 22 in MAI → 22 is not the filename it is a folder with the number of the day. So "13_lq_06_2003-05-22.pdf " has to be stored in the folder “22"” in the folder “MAI” in the folder “1SEM2003” in the folder “LQ” in the folder “PDF_Archiv” on the volume “SOC_Archiv”

and “17_so_05_2003-05-02.pdf” has to be stored in the folder “02"” in the folder “MAI” in the folder “1SEM2003” in the folder “SOGR” in the folder “PDF_Archiv” on the volume “SOC_Archiv”

1SEM2003 means the first half of the year 2003 in July there is a new folder 2SEM2003 with the folders of the months July to December

Roland

If you don’t mind, I have a related script that I’d like to share. It’s a bit messy, but it might help someone.

The script will take a folder full of files and sort them into sub-folders based on their names. It actually strips off the file extension and any numberical part and compares the names, so it might not be applicable to the current question, but it could be modified to compare the first characters/numbers of the name.

I’ve commented it throughout so you can see the logic. It could also probably be simplified a little, but it currently works for my prorposes.

(*

This is an AppleScript which will organize a folder-full of files into sub-folders based on the file names. It will remove any numbers, dashes, spaces and file extensions and compare what remains.

If there is more that one file in the series, a new folder will be created and all similar files will be placed into the folder.

If there is only one file (of if the file name is just numbers) then the file will be put into the ‘other’ folder.

For example:

George_Washington_01.jpg
George_Washington_02.jpg
George_Washington_03.jpg
George_Washington_22.jpg
Martin Van Buren 22.tif
Martin Van Buren 24.tif
Martin Van Buren 35.tif
Andrew Jackson 01.jpg
01-01234-03.tif

The George_Washington files will be put into a “George Washington” folder; Martin Van Buren into “Martin Van Buren” folder; but Andrew Jackson and 01-01234-03 will be put into the “Other” folder.

*)

------------ Initialize variables

set previous_item to “”
set this_item to “”
set short_this_item to “”

------------ Choose Source Folder & get list of visible files
set the source_folder to choose folder with prompt “Folder containing items to organize:”
set the the_list to list folder source_folder without invisibles
set source_folder to source_folder as string

------------ Set the variables for first item (since there is no previous item)
set next_item to (extract_text(item (1) of the_list)) ----- simplify the file name for comparison

------------ Loop through the list
set total_items to number of items in the_list

tell application “Finder”
activate
–open folder source_folder
–set current view of window 1 to list view
end tell

repeat with i from 1 to total_items

------------ Set current item
copy item i of the_list to this_item ----- get the current file name
set this_item_file to (source_folder & this_item) as alias ---- get the file path
set this_info to info for this_item_file --- get info (to check that it's not a folder)

if the folder of this_info is false then ------------ Make sure it's not a folder
	
	set previous_item to short_this_item ----- move the current file name to the previous name spot
	set short_this_item to next_item ----- make the next file name the current file name 
	
	if i != total_items then --- make sure it's not the last item
		set next_item to (extract_text(item (i + 1) of the_list)) --- get the next item's short (extracted) name
	else
		set next_item to ""
	end if
	
	tell application "Finder"
		
		--Uncomment the next two lines if you want the source folder to open in the Finder
		--open folder source_folder
		--set current view of window 1 to list view
		
		if short_this_item = previous_item then ----- is the short name the same as the previous short name?
			try
				move file this_item_file to folder (source_folder & short_this_item as string) --- if it's the same as previous, move the file to the exisiting folder
			end try
		else if short_this_item = next_item then ------ is the short name the same as the next file's short name? Then try to make a folder for them 
			if not (exists folder (source_folder & short_this_item as string)) then --- does a folder already exist for it?
				if not (exists file (source_folder & short_this_item as string)) then ---- does the folder name confict with a file name?
					
					set new_folder to make new folder at alias source_folder with properties {name:short_this_item} --- make new folder
				else
					set new_folder to make new folder at alias source_folder with properties {name:short_this_item & " f"} --- make new folder (& 'f") 
				end if
				try
					move file this_item_file to new_folder ---- move the file to the new folder
				end try
			else
				try
					move file this_item_file to folder (source_folder & short_this_item as string) ---- move this file to the exisiting folder
				end try
			end if
			
		else --- if the short name doesn't match the previous or next file, move it to the 'other' folder
			if not (exists folder (source_folder & "Other" as string)) then --- does the 'other' folder exist?
				set new_folder to make new folder at alias source_folder with properties {name:"Other"} --- make an 'other' folder
				try
					move file this_item_file to new_folder ---- move the file
				end try
			else
				try
					move file this_item_file to folder (source_folder & "Other" as string) --- move the file
				end try
			end if
		end if
		
		
	end tell
end if

end repeat
beep 2

on extract_text(current_name) ------ calculate the short name
copy “” to new_name
copy current_name to new_name

repeat with search_string in (every item of "0123456789!#$'+%()") ---- remove numers and some other characters
	if new_name = " " or new_name = "" then return current_name
	if new_name contains search_string then
		set AppleScript's text item delimiters to the search_string
		set the text_item_list to every text item of the new_name
		set AppleScript's text item delimiters to the ""
		set the new_name to the text_item_list as string
	end if
end repeat

--display dialog new_name --------------------**************

repeat with search_string in (every item of "_-") ----- replace these characters with spaces
	if new_name = " " or new_name = "" then return current_name
	if new_name contains search_string then
		set AppleScript's text item delimiters to the search_string
		set the text_item_list to every text item of the new_name
		set AppleScript's text item delimiters to the " "
		set the new_name to the text_item_list as string
		set AppleScript's text item delimiters to the ""
	end if
end repeat

--display dialog new_name --------------------for testing purposes

--- Make sure the short name is not just the file extension (may occur if file name is only numbers
repeat with search_string in {every item of "", " ", ".jpg", ".gif", ".tif", ".jpeg", ".uu", ".mim", ".mime", ".txt"}
	if new_name = " " or new_name = "" then return current_name
	if new_name = search_string then
		return current_name
	end if
end repeat

--display dialog new_name --------------------for testing purposes

if new_name = ".jpg" then ------ is the name just a number and extension?
	return current_name
else
	repeat with search_string in {every item of ".jpg", ".gif", ".tif", ".jpeg", ".uu", ".mim", ".mime", ".txt"} ------ remove these file extentions
		if new_name = " " or new_name = "" then return current_name
		if new_name contains search_string then
			set AppleScript's text item delimiters to the search_string
			set the text_item_list to every text item of the new_name
			set AppleScript's text item delimiters to the ""
			set the new_name to the text_item_list as string
			set AppleScript's text item delimiters to the ""
		end if
	end repeat
	
	set AppleScript's text item delimiters to the "  " -------- remove double spaces
	set the text_item_list to every text item of the new_name
	set AppleScript's text item delimiters to the " "
	set the new_name to the text_item_list as string
	set AppleScript's text item delimiters to the ""
	
	--display dialog new_name --------------------for testing purposes
	
	repeat while last character of new_name = " " ----- remove spaces at the end
		if new_name = " " or new_name = "" or new_name = "." then return current_name
		set new_name to characters 1 thru -2 of new_name as string
	end repeat
	
	repeat while last character of new_name = "." ----- remove periods at the end
		if new_name = "." or new_name = "" or new_name = "." then return current_name
		set new_name to characters 1 thru -2 of new_name as string
	end repeat
	
	--display dialog new_name --------------------for testing purposes
	
	return new_name
end if

end extract_text

Nearly final version




property soc_archiv_path : "Macintosh HD:Users:guyhail:Temporary:SOC_Archiv:PDF_Archiv:" -- highest level directory for destination of files
property source_path : "Macintosh HD:Users:guyhail:Temporary:SOC_Archiv:PDF_Source:" -- path to folder containing the pdf files that need sorted
-- IMPORTANT: paths names are text
-- if aliases are used, the code below must be changed to handle switching between
-- aliases file paths and text file paths; e.g., 
-- if variable 'fp' contains an alias to a folder, 
-- then 'folder fp' will fail because finder knows 'fp' is a folder
property first_half_year : "1SEM"
property second_half_year : "2SEM"
-- file names are structured "13_lq_06_2003-05-22.pdf"
-- underscore separates major parts
-- dash separates date
-- period separates extension
-- I prefer to name these instead of hard-coding the dash, period, and underscore characters
property fn_separator : "_"
property date_separator : "-"
property extension_separator : "."
property directory_separator : ":" -- I use the ":", but may have to change to "/"
-- applescript is not a complete programming language and so lacks features
-- it has no command to change case;  below will make up for this
property uppercase : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property lowercase : "abcdefghijklmnopqrstuwvxyz"
-- I don't think there is a simple way to change month numbers to month text
-- I am not that familiar with date functions in applescript
-- and it's possible the date functions return only English names of months, which you may not want
property all_months : {"JAN", "FEB", "MAR", "APR", "MAI", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"} -- change to proper abbreviations

-- MAIN BODY
-- uses finder for name and move move functions, may need to use "system events" instead
-- I'm not sure why apple has both 'finder' and 'system events' applications
tell application "Finder" to set all_pdf_files to every file of folder source_path whose name contains ".pdf" -- get all pdf files from source folder
repeat with one_pdf_file in all_pdf_files -- examine each pdf file one at a time
	tell application "Finder" to set current_file_name to name of (contents of one_pdf_file) --  may need to use unicode
	-- set current_file_name to "13_lq_06_2003-05-22.pdf" -- single instance for testing; comment out for final version; to test comment out repeat/end repeat
	set old_delimiters to AppleScript's text item delimiters -- store current text delimiters
	set AppleScript's text item delimiters to extension_separator -- set text delimiters to what we want
	set file_name_only to first text item of current_file_name -- chop off .pdf WARNING assumes only 1 "." in file name
	set AppleScript's text item delimiters to fn_separator -- set text delimiters to what we want
	set file_name_parts to every text item of file_name_only
	-- if file name is "13_lq_06_2003-05-22.pdf" then
	-- file_name_parts = {"13", "lq", "06", "2003-05-22"}
	-- item 2 is "lq", "so", "sogr", "bt", etc.
	-- last item is the date, for example: "2003-05-22"
	set AppleScript's text item delimiters to old_delimiters -- restore text delimiters before calling subroutines, which may alter the delimiters
	set altered_path to my correct_abbreviation(item 2 of file_name_parts) -- see handler for function
	set new_destination to make_new_path at altered_path for last item of file_name_parts -- full path to where file will be moved
	-- display dialog new_destination -- debug; comment out for final version
	set path_exists_flag to my does_folder_exist(new_destination) -- see handler for function
	if path_exists_flag then
		-- if the full path to the file's new location exists or was created
		-- move the file
		-- move should 'move' not 'copy', file should not be left in original location
		tell application "Finder" to move (contents of one_pdf_file) to folder new_destination replacing no
		-- one_pdf contains the full path (as a finder file specification -fss) to the pdf file we want to move
		-- 'replacing no' DO NOT replace an already-existing file of the same name
		-- if files with the same name may exists, additional code should be written here
	end if
end repeat

-- HANDLERS (subroutines)

-- following two handlers are for creating directories or confirming directory hierarchy exists
on does_folder_exist(this_path)
	-- takes a full path terminating in a folder (not a file)
	-- confirms that every directory in the path exists
	-- and creates (through the sub-routine 'create heirarchy') directories as necessary
	-- returns true (path exists) or false (path does not exist)
	create_folder_heirarchy of me at this_path for "" -- call recursive handler to create necessary sub-directories
	try
		set folder_exists to (info for alias this_path)'s folder
	on error
		set folder_exists to false
	end try
	-- if not folder_exists then display dialog "path creation failed" -- debug only; comment out for final version
	return folder_exists
	-- return a BOOLEAN (true/false) value
	-- TRUE is the full path exists (or was created)
	-- FALSE if some error occured
end does_folder_exist

to create_folder_heirarchy at this_path for next_folder
	-- recursive handler for creating directories; returns nothing
	-- uses global properties directory_separator
	-- given a path and a folder name, check if the path exists
	-- if it the path exists, create the folder,
	-- if it the path does not exist
	--	this routine calls itself using the last item in the path as the folder name
	--	and the path (now missing the last folder name) as the new 'this_path'
	set old_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to directory_separator
	try
		tell application "Finder" to set shorter_path to (container of alias this_path) as text
		-- finder is asked for the path to the last directory in this_path
		-- for example: 
		--	if this_path is "HD:f1:f2:f3:" then
		--	then shorter_path is now "HD:f1:f2:"
		-- if "HD:f1:f2:f3" does not exist, an error occurs
		-- which is trapped by the try statement
		--
		-- the 'if' below is executed only if an error did not occur
		-- that is, if this_path exists
		-- if 'this_path' exists and next_folder has something in it, then create a folder named next_folder
		-- otherwise, do nothing (e.g., when next_folder is empty, the full path exists)
		if (next_folder is not "") then
			try
				tell application "Finder" to make new folder at alias this_path with properties {name:next_folder}
			on error errmsg
				display dialog errmsg
			end try
		end if
	on error
		set current_folder to last text item of (text items of this_path) -- unfortunately can't use the finder for this, is it operates only on existing paths
		set shorter_path to (items 1 thru -2 of (text items of this_path)) as text
		-- if the attempt to 'set shorter_path' above failed, nothing is in 'shorter_path'
		-- and the finder can't be used to chop off the lowest level directory, because the path doesn't exist
		-- so text items must be used instead; be certain the correct directory separator is used
		-- (see the applescript text items assignment statement, above)
		create_folder_heirarchy of me at shorter_path for current_folder
		-- call this handler recursively, trying to create next highest level in heirarchy
		-- when the recursive calls are complete, all the higher level folders should exist
		-- then create missing folder, since all the recursive calls return here,
		-- all missing folders will be created
		-- one at time starting with the highest level missing folder
		-- IMPORTANT: no error checking! 
		-- if the finder fails to create a folder, this whole program will crash, so watch your UNIX permissions.
		-- display dialog next_folder -- warning: puts up empty dialog box when no new folder needs created
		if (next_folder is not "") then
			tell application "Finder" to set x to make new folder at folder this_path with properties {name:next_folder}
		end if
	end try
	set AppleScript's text item delimiters to old_delimiters
end create_folder_heirarchy

-- following handlers create paths from a file name

to correct_abbreviation(this_abbreviation)
	-- this subroutine maps 'so' onto 'sogr'
	-- similar corrections should be added here
	if this_abbreviation is "so" then
		set altered_text to "sogr"
	else
		set altered_text to this_abbreviation
	end if
	return altered_text
end correct_abbreviation

to make_new_path at this_directory for this_date
	-- uses global properties soc_archiv_path
	-- subroutines use global properties date_separator, uppercase, lowercase, all_months
	-- display dialog this_directory -- for debug purposes
	-- display dialog this_date -- for debug purposes
	set temp_path to my make_upper_case(this_directory)
	set temp_path to soc_archiv_path & temp_path & directory_separator
	-- display dialog temp_path -- for debug purposes
	set temp_path to temp_path & my add_date_paths(this_date)
	return temp_path
end make_new_path

to add_date_paths(this_date)
	-- subroutines use global properties date_separator, all_months, directory_separator, first_half_year, second_half_year
	-- adds the year to the current directory, then a month directory, then a date directory
	set old_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to date_separator
	set date_parts to every text item of this_date
	set month_number to (item 2 of date_parts) as integer --get month number; used for name & "?SEM"
	set month_name to item month_number of all_months -- convert number to name
	-- display dialog month_number -- debug only; comment out for final version
	if month_number < 7 then
		-- add "1SEM" or "2SEM"
		-- month_number 1 thru 6 is mapped to "1SEM"
		-- month_number 7 thru 12 is mapped to "2SEM"
		set year_divider to first_half_year
	else
		set year_divider to second_half_year
	end if
	set temp_path to year_divider
	set temp_path to temp_path & item 1 of date_parts & directory_separator
	set temp_path to temp_path & month_name & directory_separator
	set temp_path to temp_path & (item 3 of date_parts) & directory_separator
	set AppleScript's text item delimiters to old_delimiters
	return temp_path
end add_date_paths

-- utility handlers

to make_upper_case(this_text)
	-- use global properties date_separator, uppercase, lowercase
	set changed_case to ""
	set each_char to every character of this_text
	set uc_chars to every character of uppercase
	repeat with one_char in each_char
		set changed_case to changed_case & item (offset of (contents of one_char) in lowercase) of uc_chars
	end repeat
	return changed_case
end make_upper_case

I tried to copy and paste the values Yellowcab posted to create a script in Script Editor, but it kept coming back with errors. Since I don’t have enough knowledge to figure out how to fix the errors but would like to have a working copy of a script that reads the names of files in a folder and sorts them into folders it creates based on the names of files that have similar names, I wondered if anyone could help me by uploading a working version of that script.