Rename files with its folder name


I have a parent folder with many subfolders inside.
Each subfolder has its own files.

I need to add to every file part of its subfolder name.

My basic knowledge of applescript is beyond this. Very grateful for your help.

My original information is like this one:

Result desired:

Every subfolder name is separated by one space, the part that is before that space is what I need to add to the beginning of every file name.

Searching the forum I found one script of Craig Smith that maybe would suit I am looking for.

Huge thank you very much for your help.

Model: mac mini 2011
AppleScript: 2.6.1
Browser: Safari 537.36
Operating System: Mac OS X (10.9)

As you are running 10.9, I will not use ASObjC but old fashioned Applescript.

# for

-- set theFolder to (path to desktop as text) & "Docs" # defines a text object
set theFolder to (choose folder with prompt "Select a folder:") # defines an alias object

tell application "System Events"
	if class of theFolder is alias then
		set theSubFolders to folders of theFolder
		set theSubFolders to folders of folder theFolder
	end if
	repeat with aSubFolder in theSubFolders
		set itsBeg to item 1 of my decoupe(name of aSubFolder, space)
		log itsBeg
		set theFiles to (files of aSubFolder whose visible is true)
		repeat with aFile in theFiles
			set name of aFile to itsBeg & name of aFile
		end repeat
	end repeat
end tell


on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 février 2017 18:33:16

Thank you very much, Yvan!

Now, it works flawlessly.


Thanks for the feedback.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 14:53:42