Automate moving student files into student folders by name

I'm a Middle School math teacher with the following recurring task. I scan in a student's written work for the week into a pdf file with their name and a number (standing for the week of the grading period). I have 42 files in a directory. I have 42 sub-folders, one for each student, in the folder student work. Each week, I would like to copy the files of weekly work into the appropriate student folder.


For example, the file fred 2 needs to go into folder fred and the file amy 2 needs to go into folder Amy. The following week, it will be the same, except with fred 3 into folder fred and Amy 3 into folder amy.


It's not an odious task but since it comes every week, it would be great to automate. Thanks.

MacBook Air 13″, macOS 11.4

Posted on Mar 3, 2023 8:51 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 3, 2023 10:34 AM

Almost any system here is going to rely heavily on the file naming.As long as you're consistent with your naming, there shouldn't be any problem.


Here's an AppleScript solution (although there are other ways to solve this).


You can paste this script into a Script Editor document, or into an Automator workflow using the 'Run AppleScript' action.


When run it will ask you for a folder (this can be changed to use a preset folder if you like). It then loops through the files in that folder, looking for a subfolder that matches the first word in the file name (e.g. 'fred 2.jpg' will look for a 'fred' folder. If a match is found, the file is moved into that folder.

Any file names with non-matching folder names are left as-is.


-- prompt the user for the folder to process
set topFolder to (choose folder)

tell application "Finder"
	-- get a list of files in this folder
	set theFiles to every file of folder topFolder
	-- iterate through the files
	repeat with each_file in theFiles
		-- get the file name
		set fn to name of each_file
		-- get the first word of the file name
		set first_name to (word 1 of fn)
		-- do we have a corresponding folder?
		if exists folder first_name of folder topFolder then
			-- if so, move the file into the folder
			move each_file to folder first_name of folder topFolder
		end if
	end repeat
end tell


for ease, you could save the script/workflow as an application, so you just need to double-click the script to make it run.

3 replies
Question marked as Top-ranking reply

Mar 3, 2023 10:34 AM in response to keelert

Almost any system here is going to rely heavily on the file naming.As long as you're consistent with your naming, there shouldn't be any problem.


Here's an AppleScript solution (although there are other ways to solve this).


You can paste this script into a Script Editor document, or into an Automator workflow using the 'Run AppleScript' action.


When run it will ask you for a folder (this can be changed to use a preset folder if you like). It then loops through the files in that folder, looking for a subfolder that matches the first word in the file name (e.g. 'fred 2.jpg' will look for a 'fred' folder. If a match is found, the file is moved into that folder.

Any file names with non-matching folder names are left as-is.


-- prompt the user for the folder to process
set topFolder to (choose folder)

tell application "Finder"
	-- get a list of files in this folder
	set theFiles to every file of folder topFolder
	-- iterate through the files
	repeat with each_file in theFiles
		-- get the file name
		set fn to name of each_file
		-- get the first word of the file name
		set first_name to (word 1 of fn)
		-- do we have a corresponding folder?
		if exists folder first_name of folder topFolder then
			-- if so, move the file into the folder
			move each_file to folder first_name of folder topFolder
		end if
	end repeat
end tell


for ease, you could save the script/workflow as an application, so you just need to double-click the script to make it run.

Mar 3, 2023 10:22 AM in response to keelert

Choose Utilities from the Finder's Go menu, open the Script Editor, and run a script such as:


tell application "Finder"

set the_folder to folder "Desktop" of home

repeat with this_file in (get files of the_folder)

move this_file to folder (word 1 of (get name of this_file)) of the_folder

end repeat

end tell


(235687)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automate moving student files into student folders by name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.