Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Sort files by spreadhseet

I have a folder than contains over 1000 text files. The text files are supposed to be organised in folders.


I have a spreadsheet which states which text file goes in which folder:



Is there anyway I can make the "Finder" respond to this spreadsheet, so it puts the correct file in the correct folder. Maybe something involving Applescript or the terminal?


It's just a simple "Apple Numbers" spreadsheet, so I can copy and paste the data out into something else.

MacBook Pro Retina

Posted on Jul 12, 2019 9:44 AM

Reply
Question marked as Best reply

Posted on Jul 12, 2019 2:46 PM

Tell me about that lost hour of my time, when a solution already exists on Ask Different.


Here is an AppleScript that I have tested on macOS Mojave 10.14.5 using your example .csv data. It will prompt you twice: 1) the .csv file, and 2) the folder containing your text files. After that, it is on autopilot and reads in the headerless .csv file, and proceeds to process each row, and move the files that exist into their designated folders. If the folder does not exist, it will make the folder and then move the file.


-- acsv.applescript

-- Prompt for headerless, .csv file and starting folder containing the files to process.
-- Reads each row of the CSV into a list, splits that list item into file and folder, and
-- moves the file into the appropriate folder. If a file does not exist, it skips that
-- row processing, and if the folder does not exist, it will make the folder.
-- Tested: macOS 10.14.5
-- Reference: https://discussions.apple.com/thread/250480620
-- VikingOSX, 2019-07-12, Apple Support Communities, No warranties expressed or implied.


property delim : {","}
property aDesktop : (path to desktop) as alias
property csvType : {"public.comma-separated-values-text", "public.delimited-values-text"}

set rowItems to {}
set afile to ""
set afolder to ""

set csvFile to (choose file with prompt "Select CSV file" of type csvType default location aDesktop without invisibles, multiple selections allowed and showing package contents)

set start_folder to (choose folder with prompt "Text File Folder" default location aDesktop without invisibles, multiple selections allowed and showing package contents)

set csvList to (read csvFile as text using delimiter linefeed) as list
tell application "Finder"
	repeat with arow in csvList
		repeat 1 times
			-- use as many variables as you have columns in the CSV
			set {afile, afolder} to my csvParse(arow, delim) as list
			log afile
			log afolder
			try
				set check_file to (start_folder & afile) as text as alias
			on error
				exit repeat
			end try
			try
				set check_folder to (start_folder & afolder) as text as alias
			on error
				make new folder at start_folder with properties {name:afolder}
				set check_folder to (start_folder & afolder) as text as alias
			end try
			move file check_file to folder check_folder
		end repeat
	end repeat
end tell
return

on csvParse(nrow, adelim)
	-- replaces the commas in each row of the CSV with newlines
	-- returns columns of data
	return paragraphs of (do shell script "sed -E 's/,/\\'$'\\n/g' <<<" & nrow's quoted form)
end csvParse


Similar questions

12 replies
Question marked as Best reply

Jul 12, 2019 2:46 PM in response to big_smile

Tell me about that lost hour of my time, when a solution already exists on Ask Different.


Here is an AppleScript that I have tested on macOS Mojave 10.14.5 using your example .csv data. It will prompt you twice: 1) the .csv file, and 2) the folder containing your text files. After that, it is on autopilot and reads in the headerless .csv file, and proceeds to process each row, and move the files that exist into their designated folders. If the folder does not exist, it will make the folder and then move the file.


-- acsv.applescript

-- Prompt for headerless, .csv file and starting folder containing the files to process.
-- Reads each row of the CSV into a list, splits that list item into file and folder, and
-- moves the file into the appropriate folder. If a file does not exist, it skips that
-- row processing, and if the folder does not exist, it will make the folder.
-- Tested: macOS 10.14.5
-- Reference: https://discussions.apple.com/thread/250480620
-- VikingOSX, 2019-07-12, Apple Support Communities, No warranties expressed or implied.


property delim : {","}
property aDesktop : (path to desktop) as alias
property csvType : {"public.comma-separated-values-text", "public.delimited-values-text"}

set rowItems to {}
set afile to ""
set afolder to ""

set csvFile to (choose file with prompt "Select CSV file" of type csvType default location aDesktop without invisibles, multiple selections allowed and showing package contents)

set start_folder to (choose folder with prompt "Text File Folder" default location aDesktop without invisibles, multiple selections allowed and showing package contents)

set csvList to (read csvFile as text using delimiter linefeed) as list
tell application "Finder"
	repeat with arow in csvList
		repeat 1 times
			-- use as many variables as you have columns in the CSV
			set {afile, afolder} to my csvParse(arow, delim) as list
			log afile
			log afolder
			try
				set check_file to (start_folder & afile) as text as alias
			on error
				exit repeat
			end try
			try
				set check_folder to (start_folder & afolder) as text as alias
			on error
				make new folder at start_folder with properties {name:afolder}
				set check_folder to (start_folder & afolder) as text as alias
			end try
			move file check_file to folder check_folder
		end repeat
	end repeat
end tell
return

on csvParse(nrow, adelim)
	-- replaces the commas in each row of the CSV with newlines
	-- returns columns of data
	return paragraphs of (do shell script "sed -E 's/,/\\'$'\\n/g' <<<" & nrow's quoted form)
end csvParse


Jul 13, 2019 4:10 AM in response to VikingOSX

@VikingOSX

Thank you so much, that works perfectly! Please accept my most sincere apologies about Ask Different. I posted on both at the same time. I didn't realise that double posting was an issue, but I can now see that it is bad as if people respond on both places, it causes people to waste their time, which is not right. This just hadn't crossed my mind before, but now that I know, I will take care not to do this in future (or at least link to both, so a potential respondent is aware).


Thanks again, you have been a big help and I greatly appreciate your efforts.

Jul 13, 2019 4:16 AM in response to rccharles

@rccharles

Sorry, I posted on both at the same time. Although it should have been obvious, It didn't cross my mind how rude that would be, as I have wasted the time of the people who have helped me. Now that I know, I will take great care not to do it again (Or at least link the two posts, so potential helpers know not to waste their time if a solution is provided on one site). Thank you kindly for pointing this out, as I do feel bad for the wasted time I have caused, especially because people on both sites have gone out of their way to help. Thanks again for explaining it to me.

Jul 13, 2019 5:51 AM in response to big_smile

This is the correct support community if you want your post to get maximum exposure before it gets pushed to another page from posting activity. The Bash script response that you received on Ask Different was not an unknown solution here.


For those of us that do respond with coding solutions, it is simply about time allocation to get to your request. I could have offered the same Bash script, or a solution written in several scripting languages that would have been far less coding and faster, yet remain a Terminal intimidation factor for others that may want a similar solution.


You can remove the two log statements inside that repeat 1 times loop as they are for debugging and unnecessary.



Jul 13, 2019 11:57 AM in response to rccharles

If check_file assigment fails in the inner repeat loop because the filename from the CSV row is missing, then I want to exit that repeat the inner iteration, as all other processing for that row is pointless. The inner exit repeat throws processing to the outer repeat loop, where I can fetch the next row. If this was a real programming language, there would be familiar continue and break clauses instead.

Sort files by spreadhseet

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