make a folder with same name in every subfolder

I can’t figure out a way to make a folder “Originaux” in every folder of a choosen folder.

I know how to make the folder with the proper name, but not how to go into each subfolder to create it.

I have this right now, which work for just the selected folder.


tell application "Finder"
	set theFolder to selection as string
	
	make new folder at (alias (theFolder)) with properties {name:"Originaux"}
end tell

Any help appriciated!

Jeff

allF in the following script is a list of aliases to every folder in the selected folder.


tell application "Finder"
	set T to get selection as alias
	set allF to folders of entire contents of T as alias list
end tell

You can chug through them in a repeat loop and make new folder in each.

thanks, but I can’t figure it out.

I’ve tried this amongst many other, and still get an error.


tell application "Finder"
	set T to get selection as alias
	set allF to folders of entire contents of T as alias list
	repeat with i from 1 to count of allF
		set theFolder to item i of allF
		make new folder at (alias (theFolder)) with properties {name:"Originaux"}
	end repeat
end tell


tell application "Finder"
	set T to get selection as alias
	set allF to folders of entire contents of T as alias list
	repeat with aF in allF
		make new folder at contents of aF with properties {name:"Originaux"}
	end repeat
end tell

To use your version remove the alias (it failed because allF is a list of aliases, and you were
referencing an alias to an alias)


tell application "Finder"
	set T to get selection as alias
	set allF to folders of entire contents of T as alias list --> Note the items in this list are aliases already
	repeat with i from 1 to count of allF
		set theFolder to item i of allF
		make new folder at theFolder with properties {name:"Originaux"}
	end repeat
end tell

Perhaps I should add that the ‘tricks’ here are that in a Finder block, ‘entire contents of’ digs down, so to get all the files in a folder, for example, you say:


tell application "Finder" to set allTheFiles to files of entire contents of myFolder -- where myFolder is an alias to a folder

which would produce a list of Finder references to the files. To get aliases to all those files, you add ‘as alias list’ as I did in the examples above.

Adam,

Both script (yours and my version modified) fail, they give me a Can’t make every «class cfol» of «class ects» error. Any ideas why?

Thanks for your help!

Jeff

hi jeff, adam,

might i suggest some recursion? i don’t know if you need to get inside a folder within a folder, within a folder, but this would do it:


global TotalCount

tell application "Finder"
	activate
	set chosenFolder to (choose folder with prompt "Add folder \"Originaux\" to each nested folder?")
	set TotalCount to 0
	my folderprocess(chosenFolder)
	activate
	beep 2
	display dialog ("Total folders added: " & TotalCount) buttons "OK" default button "OK"
end tell

on folderprocess(xFolder)
	tell application "Finder"
		set subFolders to folders of xFolder -- Get a list of the subfolders of the folder you picked
		set theFolder to xFolder as string
		make new folder at (alias (theFolder)) with properties {name:"Originaux"}
		set TotalCount to (TotalCount + 1)
		if (count of subFolders) ≠ 0 then -- If folders are found				
			repeat with thisFolder in subFolders -- Start repeating with a new list of subfolders
				my folderprocess(thisFolder) -- send it back to the beginning of the handler
			end repeat
		end if
	end tell
end folderprocess

NOTE: adapted from BLUEFROG’s script here:

http://bbs.applescript.net/viewtopic.php?id=16002

Ok, my mistake, the script works if I select a folder containing other folders, but not if I select some folder whom I want to add the folder Originaux to be added.

The following works


tell application "Finder"
	try
		set T to get selection as alias
		set allF to folders of entire contents of T as alias list
		repeat with aF in allF
			try
				make new folder at contents of aF with properties {name:"Originaux"}
			end try
		end repeat
	on error
		display dialog "select a folder containing other folders"
	end try
end tell

But it doesn’t create the folder “Originaux” if I select a couple of folders which don’t have any folders in them. How can I make it work with what ever situation it enconters?

Jeff

Waltr,

Your version is pretty cool, except that I need it to either create the folder at the first level if no other folders are in the folders selected, or if the seleted folder contains folder, only one level needs to have the new folder created, but not the folder containing all other folders. Does this make any sense?

Thanks, I’ll keep it as an exemple though, pretty cool.

Jeff

hi jeff,

not sure i understand you completely, but here is a stab at it:


global TotalCount

tell application "Finder"
	activate
	set chosenFolder to (choose folder with prompt "Add folder \"Originaux\" to each nested folder?")
	set TotalCount to 0
	set subFolders to folders of chosenFolder
	if (count of subFolders) = 0 then
		my folderprocess(chosenFolder)
	else
		set x to 1
		set y to (count of subFolders)
		repeat while x ≤ y
			my folderprocess(item x of subFolders)
			set x to (x + 1)
		end repeat
	end if
	activate
	beep 2
	display dialog ("Total folders added: " & TotalCount) buttons "OK" default button "OK"
end tell

on folderprocess(xFolder)
	tell application "Finder"
		set theFolder to xFolder as string
		make new folder at (alias (theFolder)) with properties {name:"Originaux"}
		set TotalCount to (TotalCount + 1)
	end tell
end folderprocess

EDITED: made it cleaner.

Alternatively, you can try something like this:

choose folder with prompt "Create "Originaux" subfolders for this folder:"
set originalFolder to POSIX path of result

do shell script "cd " & quoted form of originalFolder & ¬
	"; /usr/bin/find . -type d -maxdepth 1 -not -name '.' " & ¬
	"-execdir /bin/mkdir \"./{}/Originaux/\" \\; -print"
	
if result is "" then do shell script "/bin/mkdir -p " & quoted form of (originalFolder & "Originaux/")

Thanks to all who have contributed to this thread, I’ve taken each script and will learn from them. The job that required that needed to be finished earlier, so I had to use my version which created the folder but only in the selected folder, I had about 90 folders, so it wasn’t so bad, and still faster than trying to figure it out completely on my own.

You guys are excellent, thanks!

Jeff