Create folder and move files depending on data in CSV file

I have one large folder with thousands of mp3 files.
I want to create sub folders and move the mp3 files to these folders.
I have a CSV file that have all file names and info about what folder to add the file to.

CSV file looks like this. Note that some folders will contain several files.

“filename”,“foldername”
“examplefile1.mp3”,“examplefolder1”
“examplefile2.mp3”,“examplefolder1”
“examplefile3.mp3”,“examplefolder2”
“examplefile4.mp3”,“examplefolder3”

So I need a script that will read the CSV file line by line, create folder if not existing and move file to the corresponding folder.

Doable?
Greatful for any hints!

Hi,

boring for the cracks i think :wink:

set baseDir to "" & (choose folder with prompt "Please choose the baseFolder to operate on.")
set theCsvArray to paragraphs of (read (choose file))

set ASTID to AppleScript's text item delimiters
repeat with i from 1 to count of theCsvArray
	try
		set theItem to item i of theCsvArray
		set AppleScript's text item delimiters to ","
		set fileName to text 2 thru -2 of (text item 1 of theItem)
		set folderName to text 2 thru -2 of (text item 2 of theItem)
		set newFolder to baseDir & folderName
		do shell script "mkdir -p " & quoted form of POSIX path of newFolder
		set oldFile to baseDir & fileName
		set newFile to baseDir & folderName & ":" & fileName
		do shell script "mv -f " & quoted form of POSIX path of oldFile & space & quoted form of POSIX path of newFile
	on error e
		display dialog e giving up after 5
	end try
end repeat
set AppleScript's text item delimiters to ASTID


Hope it’ll work

Phenomenal, it works like a charm!
Thank you very much!
:smiley:

thx for replying :slight_smile:

Actually, You helped me last time I posted something here as well:)
http://macscripter.net/viewtopic.php?id=37058

Hans-Gerd Classen, can you make this script work with part of the name instead of complete names? My CVS looks like this:

my file list looks like this:

Thank you