How do i process subfolders

I have this script below that i want to run on all files nested in sub folders what do i need to add

i could prob. use some help cleaning up the code as well

any sugestions would be greatly appreciated

thanks in advance

tell application "Finder" to set thefolder to (folder of the front window) as alias
--set thefolder to (choose folder with prompt "choose a folder to comment-ize") as text
set thelist to list folder thefolder
repeat with i from 1 to count thelist
	set this_item to item i of thelist
	tell application "Finder"
		set theFile to (thefolder & this_item) --set theinfo to info for (thefolder & theFile)
		set filepath to (thefolder & theFile)
		set thisinfo to info for file theFile
		set filename to name of thisinfo
		set thecomment to the comment of file theFile
		set comment of file theFile to (thecomment & return & "Original File Name: " & (filename) as text)
	end tell
end repeat

Hi :slight_smile:
Here one suggestion (for MacOsX only):

tell application "Finder"
	set thefolder to (folder of the front window) as alias
	--set thefolder to (choose folder with prompt "choose a folder to comment-ize") as text 
	set thelist to entire contents of thefolder
	repeat with theFile in thelist
		set filename to name of theFile
		set thecomment to the comment of theFile
		set comment of theFile to ¬
			(thecomment & return & "Original File Name: " & (filename) as text)
	end repeat
end tell

:wink:

Basic pattern for processing folders recursively; adapt to suit:

on processFile(fileRef)
	(* process a Finder file reference
			fileRef : Finder reference -- the file to process
	*)
	-- your code goes here
end processFile

on processFolder(thisFolder)
	(* traverse a folder tree, calling processFile for each file found
			thisFolder : alias or Finder reference -- the folder to process
	*)
	tell application "Finder"
		set filesList to every file of thisFolder
		set foldersList to every folder of thisFolder
	end tell
	repeat with fileRef in filesList
		processFile(fileRef)
	end repeat
	repeat with folderRef in foldersList
		processFolder(folderRef)
	end repeat
	return
end processFolder

processFolder(choose folder with prompt "Choose folder to process:")

Tip: Don’t go mixing Standard Additions ‘info for’ and ‘list folder’ commands with Finder scripting when it’s cleaner and easier to use the Finder alone; it just makes things unnecessarily complicated.

on processFile(fileRef)
	tell application "Finder"
		tell fileRef
			set comment to comment & return & "Original File Name: " & name
		end tell
	end tell
end processFile

to process_file(this_file)
	-- make sure file is right type, not locked, is visible etc.
	set x to (info for this_file)'s name
	display dialog x
end process_file

-- 'recurse' process all subfolders of a folder

to recurse(this_item)
	if (info for this_item)'s folder then -- item is folder; recurse it
		set folder_contents to list folder this_item without invisibles
		repeat with one_item in folder_contents
			set current_item to ((this_item as text) & (contents of one_item) as alias)
			my recurse(current_item) -- process subfolders
		end repeat
	else -- not a folder, must be a file (but could be a 'package', you may need to check)
		my process_file(this_item) -- process file
	end if
end recurse

-- main loop

tell application "Finder" to set thefolder to (folder of the front window) as alias
set thelist to list folder thefolder without invisibles
repeat with one_list_item in thelist
	my recurse(((thefolder as text) & contents of one_list_item) as alias)
end repeat