You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Create Folders from Filenames

Hi, I have 6,000 files in a folder where the filename includes the folder names and I'd like to parse the filename to create the folders then move the files, without the folder names, into the folders. Is there any easy way, perhaps with automator or do I need to try and write an AppleScript?


This is one of the files

dvb\Apps\e_alb\etc.html


I want to create a folder structure as below and put the file called etc.html into e_alb

dvb

Apps

e_alb


I would do it manually but there are potentially hundreds of folders.


Thank you for your help.




iMac 27″, macOS 10.15

Posted on Jun 6, 2021 8:35 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 16, 2021 11:03 AM

Automator won't do this alone. You need AppleScript (or a shell script).


When you say the existing file name is:


> dvb\Apps\e_alb\etc.html


do you mean that literally, with the backslash in the filename?


If so, this is mostly a text parsing issue to work out the directory elements, a couple of Finder commands to make the folders if they don't already exist, and a move. Something like this should get you started - it's written as an AppleScript droplet, so just paste the script into a new Script Editor document and save it as an application, then drop the files in question onto the app to process them.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on open of theFiles
	--	Executed when files are dropped on the script
	repeat with eachFile in theFiles
		set i to my processAFile(eachFile)
	end repeat
end open


on processAFile(thisFile)
	set oldTIDs to my text item delimiters
	set my text item delimiters to "\\" -- need a double-backslash to escape the single backslash
	tell application "Finder"
		-- get the file name
		set t to name of thisFile
		-- and the folder it's in
		set d to container of thisFile
		-- this breaks the filename into its components based on TIDs
		set path_elements to text items of t
		-- do we have at least two elements (e.g. 1 directory and one file name
		if (count path_elements) > 1 then
			-- iterate through the directory parts
			repeat with i from 1 to (count path_elements) - 1
				-- work out the subfolder name we're looking for?
				set subfolder_name to (item i of path_elements) as text
				-- does a subfolder of this name already exist?
				if (exists folder subfolder_name of d) then
					-- yes - use it
					set d to folder subfolder_name of d
				else
					-- no, create it
					set d to make new folder at d with properties {name:subfolder_name}
				end if
			end repeat
			-- move the file into the subdirectory
			move thisFile to d
			-- and rename it
			set name of thisFile to (item -1 of path_elements)
		end if
	end tell
	set my text item delimiters to oldTIDs
end processAFile

on run
	display dialog "Drop files on this applet to move them into subdirectories"
end run


2 replies
Question marked as Top-ranking reply

Jun 16, 2021 11:03 AM in response to iwaddo

Automator won't do this alone. You need AppleScript (or a shell script).


When you say the existing file name is:


> dvb\Apps\e_alb\etc.html


do you mean that literally, with the backslash in the filename?


If so, this is mostly a text parsing issue to work out the directory elements, a couple of Finder commands to make the folders if they don't already exist, and a move. Something like this should get you started - it's written as an AppleScript droplet, so just paste the script into a new Script Editor document and save it as an application, then drop the files in question onto the app to process them.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on open of theFiles
	--	Executed when files are dropped on the script
	repeat with eachFile in theFiles
		set i to my processAFile(eachFile)
	end repeat
end open


on processAFile(thisFile)
	set oldTIDs to my text item delimiters
	set my text item delimiters to "\\" -- need a double-backslash to escape the single backslash
	tell application "Finder"
		-- get the file name
		set t to name of thisFile
		-- and the folder it's in
		set d to container of thisFile
		-- this breaks the filename into its components based on TIDs
		set path_elements to text items of t
		-- do we have at least two elements (e.g. 1 directory and one file name
		if (count path_elements) > 1 then
			-- iterate through the directory parts
			repeat with i from 1 to (count path_elements) - 1
				-- work out the subfolder name we're looking for?
				set subfolder_name to (item i of path_elements) as text
				-- does a subfolder of this name already exist?
				if (exists folder subfolder_name of d) then
					-- yes - use it
					set d to folder subfolder_name of d
				else
					-- no, create it
					set d to make new folder at d with properties {name:subfolder_name}
				end if
			end repeat
			-- move the file into the subdirectory
			move thisFile to d
			-- and rename it
			set name of thisFile to (item -1 of path_elements)
		end if
	end tell
	set my text item delimiters to oldTIDs
end processAFile

on run
	display dialog "Drop files on this applet to move them into subdirectories"
end run


Create Folders from Filenames

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