Batch Change File Extensions of Chosen Folder(s)


-- script: Batch change file Extensions of chosen folder(s), and (optionally) of its subfolders
-- written: by me, right now
-- you can choose multiple folders as well

property pathList : {}
property fromExtension : "txt" -- set here the "from" extension
property toExtension : "csv" -- set here the "to" extension
property processSubfolders : false -- set to true to process subfolders as well

set aFolders to choose folder with multiple selections allowed
repeat with aFolder in aFolders
	tell application "System Events" to set end of pathList to POSIX path of aFolder
	if processSubfolders then my get_All_Folders_of_Folder(aFolder)
end repeat

repeat with folderPath in pathList
	set folderPath to quoted form of folderPath
	try -- requred to avoid processing empty lists in the "for file in" repeat loop 
		do shell script "cd " & folderPath & "; for file in *." & fromExtension & "; do mv $file `echo $file | sed 's/\\(.*\\.\\)" & fromExtension & "/\\1" & toExtension & "/'` ; done"
	end try
end repeat

-----------------------------  Recursive Handler -----------------------------------
on get_All_Folders_of_Folder(aFolder)
	tell application "System Events"
		set foldersList to folders of aFolder
		if foldersList is {} then
			set end of pathList to (POSIX path of aFolder)
		else
			repeat with aFolder in foldersList
				my get_All_Folders_of_Folder(aFolder)
			end repeat
		end if
	end tell
end get_All_Folders_of_Folder

1 Like