Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Flatten a folder's structure via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Flatten a folder's structure via AppleScript
Authored by: ChaChi on May 12, '09 10:20:19AM
Here's a script I just made out of curiosity. It works for me here on 10.5.6:
--START
--GET FINDER SELECTION
tell application "Finder"
	activate
	set selectedFolder to selection as alias
end tell

--TEMPORARILY RENAME TOP LEVEL FOLDERS TO PREVENT FILE NAME CONFLICTS
set posixFolderPath to text 1 through -2 of (POSIX path of selectedFolder)
set folderList to paragraphs of (do shell script "find " & quoted form of posixFolderPath & " -type d -depth 1")
repeat with aFolder in folderList
	set theFolder to aFolder as string
	do shell script "mv " & quoted form of theFolder & " " & quoted form of (theFolder & "-FOLDERTMP")
end repeat

--LIST ALL FILES
set fileList to paragraphs of (do shell script "find " & quoted form of posixFolderPath & " -not -name '.DS_Store' -not -type d")

repeat with aFile in fileList
	set nameList to list folder POSIX file posixFolderPath
	
	set theFile to aFile as string
	set theInfo to info for (POSIX file theFile)
	set theName to name of theInfo
	
	--RENAME FILE IF NAME ALREADY EXISTS
	if theName is not in nameList then
		set newName to theName
	else
		set theExt to name extension of theInfo
		if theExt is not missing value then
			set theExt to "." & theExt
		else
			set theExt to ""
		end if
		set theName to text 1 through -((count of theExt) + 2) of theName
		set x to 0
		repeat
			set x to x + 1
			set newName to theName & "-" & x & theExt
			if newName is not in nameList then exit repeat
		end repeat
	end if
	
	--MOVE FILE TO TOP LEVEL DIRECTORY
	do shell script "mv " & quoted form of theFile & " " & quoted form of (posixFolderPath & "/" & newName)
end repeat

--DELETE ALL FOLDERS
do shell script "find " & quoted form of posixFolderPath & " -type d -mindepth 1 -delete"
--END


[ Reply to This | # ]
Flatten a folder's structure via AppleScript
Authored by: abderrahman on May 12, '09 02:39:34PM

Yes! This works, for me. And on 10.5.7, just now installed.



[ Reply to This | # ]