Simple way to flatten folder hierarchy?

How can I specify a folder by path, and flatten the internal structure of that folder?
The files within any subfolders need to be moved to the root folder, and the empty subfolders deleted.
(I also need to retain duplicates with some kind of variant naming convention like -01, -02, etc.)

The result is a folder which contains files only, with no subfolders remaining.

Thanks for your help.

Here's an AppleScript that recursively processes a folder whose path is read from the Keyboard Maestro variable folderpath. You would set the value of folderpath at an earlier point in your macro, before executing this script, and it takes a path in slash notation, e.g. ~/Desktop/Nested Folder Tree/

AppleScript
property rootdir : a reference to system attribute "KMVAR_FolderPath"

flatten(rootdir)

to flatten(fp)
	local fp
	
	script
		use sys : application "System Events"
		property directory : sys's folder fp
		
		to flatten(f)
			local f
			if f = {} then return {}
			
			if f's class = folder then
				return flatten(f's folders)
				
			else if f's class = list then
				(files of f's item 1 whose visible = true) ¬
					& flatten(f's item 1) ¬
					& flatten(rest of f)
			end if
		end flatten
		
		to cleanup(f)
			local f
			delete f's folders
		end cleanup
		
		on uniqueName for f at fp
			local f, fp
			
			set i to 1
			set filename to f's name
			set newname to filename
			
			repeat while exists (a reference to the ¬
				file named newname in fp)
				
				set i to i + 1
				set newname to the contents of {[¬
					text items 1 thru -2 of filename, ¬
					space, i], text item -1 of filename} ¬
					as text
			end repeat
			
			newname
		end uniqueName
		
		to move fs to dir
			local fs, dir
			
			set text item delimiters to "."
			
			if fs = {} then return
			
			script |files|
				property list : fs
			end script
			
			set [f, f_] to [item 1, rest] of list of |files|
			move f_ to dir
			
			set newname to uniqueName for f at dir
			set [fpath, f's name] to [¬
				f's container, ¬
				newname & ".rename"]
			
			set f to the file named (newname & ".rename") in fpath
			tell sys to move f to dir
			set the name of the file named ¬
				(newname & ".rename") in ¬
				dir to newname
		end move
	end script
	
	tell the result
		flatten(its directory)
		move result to its directory
		cleanup(its directory)
	end tell
end flatten

It's not the fastest-running script, and I'm sure there are efficiency improvements that could be made, which others may chime in on. But, for now, it's a starting point.

Thanks! This works quite well for my purposes.