list folder alternative

I have a script that renames files in a backup directory without renaming any folders. So far so good. But using the list folder command only returns a list of top level files and folders. How could I code line 2 (set backup_files…) to include nested folders while ignoring invisibles? Thanks.

--set destination folder and get list of items in folder
set the_backup_destination to alias "Powerbook:Users:dou:Desktop:auto backup:"
set backup_files to (list folder the_backup_destination without invisibles)
set item_count to count of backup_files
--determine if item is a folder
repeat with n from 1 to item_count
	set name_of_file to item n of backup_files
	set file_ref to ("" & the_backup_destination & name_of_file) as alias
	set file_info to info for file_ref
	set folder_attribute to kind of file_info
	--rename files, exclude folders
	tell application "Finder"
		if (contents of name_of_file) does not start with "bu_" and (folder_attribute ≠ "Folder") then
			set new_filename to "bu_" & name_of_file
			set name of file_ref to new_filename
		end if
	end tell
end repeat

This script will return the names of every file in a folder including those in internal folders.

property fileNameList : {}
getEveryFileName(choose folder)
fileNameList

on getEveryFileName(theFolder)
	tell application "Finder"
		my getNames(get files of theFolder)
		set FolderList to folders of theFolder
		repeat with everyFolder in FolderList
			my getEveryFileName(everyFolder)
		end repeat
	end tell
end getEveryFileName

on getNames(FileList)
	repeat with everyFile in FileList
		try
			tell application "Finder" to set end of fileNameList to name of everyFile
		on error anError
			display dialog anError
		end try
	end repeat
end getNames

Hi guys.

Note that, if the property ‘fileNameList’ is not reset (either at the beginning or the end of the script run), any previous names will be retained - and the new names will just be added to a constantly growing list.

A cleaner, shorter and faster approach might be:

tell application "Finder" to set fileNameList to name of files of entire contents of (choose folder)

Since your folder “auto backup” appears to be on the desktop, adiallo, your file renaming routine could probably be refined to something like:

tell application "Finder" to repeat with f in (get files of entire contents ¬
	of folder "auto backup" whose name does not start with "bu_")
	tell f to set name to "bu_" & name
end repeat

If it’s somewhere else, then perhaps a slight variation:

set auto_backup to "path:to:auto backup:" (* modify as required *)
tell application "Finder" to repeat with f in (get files of entire contents ¬
	of folder auto_backup whose name does not start with "bu_")
	tell f to set name to "bu_" & name
end repeat

Adam, thanks for the script. I learned a lot by going through it.

Thanks Kai, I did indeed notice that the file list was retaining previous names. Your refinement is very helpful.

Hello.

I tried Adam’s version and Kai’s using AppleScript 2.3 under Snow Leopard 10.6.3 and timing the Scripts through Script Debugger 4.5:

Kai’s version used 44.80 seconds:


property fileNameList : {}
getEveryFileName(choose folder)
fileNameList
tell application "Finder" to set fileNameList to name of files of entire contents of (choose folder)

Adam Bell’s version used 36.07 seconds:

property fileNameList : {}
getEveryFileName(choose folder)
fileNameList

on getEveryFileName(theFolder)
	tell application "Finder"
		my getNames(get files of theFolder)
		set FolderList to folders of theFolder
		repeat with everyFolder in FolderList
			my getEveryFileName(everyFolder)
		end repeat
	end tell
end getEveryFileName

on getNames(FileList)
	repeat with everyFile in FileList
		try
			tell application "Finder" to set end of fileNameList to name of everyFile
		on error anError
			display dialog anError
		end try
	end repeat
end getNames

There were 504 filenames to be added to the list from my desktop folder.

I found the result quite interesting because I believed that Kai’s version was the fastest.

Best Regards

McUsr

Kai’s version was just this:


tell application "Finder" to set fileNameList to name of files of entire contents of (choose folder)

And if the two scripts you quoted were the actual scripts you timed, don’t forget that the timings include the time it took you to choose the folders!

Hello.

In all honesty. I first copied Adam’s Script into the Script Debugger, and ran it.
Then I added kai’s line as you can see above, then I added a return statement at the end,
thinking that that would suffice to give a true test. Well, It did not.
I wrongly deduced because of the similar variable names that Kai had just extracted his one line
from Adams.

It appears that the predefined property fileNameList in my version slowed the script down tremendously.
The gross overhead was of course Script Debugger, which inspects the contents and updates the list at all the time.
So this way of comparing the two alternatives can’t be done through Script Debugger As I can’t escape Script Debugger’s monitoring of Adam’s list.

Kai’s standalone version uses 0.13 seconds, as it is, compared to Adam’s which uses 33.35 seconds when paths are hardcoded in both scripts.

I apollogize and I’m sorry for it.

McUsr

Meanwhile Spotlight searching (/usr/bin/mdfind) is much faster than the Finder using entire contents