Copying directorytree without contents other than folders.

Hi everybody,

Once in a while I need to backup directory trees with only the folderstructure in them. All the files like word docs, jpg files etc. etc. need to be left out. Although copying an entire directory with contents is easy this mentioned issue seems to be a problem.

Any ideas?

Ruben.

With thanks to Angus McIntyre who created the general recursive code!
Recursion is very cool!
This was quite a challenge though, for a newbie like me :smiley:

This works, but I did no error checking though…

I would appreciate helpful comments from the Applescripter pros, to improve my code!

property slength : ""
property deststring : ""

on run
	set AppleScript's text item delimiters to {":"}
	
	set srcFolder to choose folder with prompt "Choose source folder that contains the hierarchy:"
	set srcstring to srcFolder as string --you can only count in a string path
	set slength to count text items of srcstring
	set destfolder to choose folder with prompt "Choose destination fplder. It should be empty:"
	set deststring to destfolder as string
	set dlenght to count text items of deststring
	processObject(srcFolder)
end run

on processObject(theObject)
	
	set theInfo to info for theObject
	if theInfo is folder then
		set theContents to list folder theObject
		
		processFolder(theObject)
		
		repeat with theItem in theContents
			set thePath to ((theObject as string) & theItem)
			set lastpart to text items slength thru last item of (thePath as string) -- get the part of the path after the source path
			set newPath to deststring & lastpart --newpath is one "path item' too long for make new folder
			set newPath to (text items 1 thru -2 of newPath) as string --so get rid of the last part
			set theItemInfo to info for (thePath as alias) --alias is needed or "info for" generates an error
			if theItemInfo is folder then -- if the item is a folder make a new folder in the destination folder (or sub folder)
				tell application "Finder" to set x to make new folder at (newPath as alias) with properties {name:theItem as string}
			end if
			processObject(thePath as alias)
		end repeat
	else
		--beep 3 --test
	end if
end processObject

on processFolder(theFolder)
	--beep -- test
end processFolder

I sent this code to rubenlaan privately, so don’t flame him if he doesn’t thank me here, he allready did! :smiley: