fastest way to get file names from a folder?

I need a list of file names from a folder. The list of names must include both visible and invisible items. And I want to find the fastest way to do this! Here’s what I have so far.

This gets the list of names I want but the problem with this is apple says the ‘list folder’ command is deprecated so I’d rather not use it.

set thePath to path to desktop folder
set allList to list folder thePath

So here’s another way to do it. The problem with this is it’s much much slower, 22 times slower on speed tests.

set thePath to path to desktop folder
tell application "System Events" to set allList to name of every item of thePath

So is there a method that’s as fast as ‘list folder’ or possibly even faster that isn’t outdated?

How about this?

set thePath to quoted form of POSIX path of (path to desktop folder)
set allList to paragraphs of (do shell script "/bin/ls -A " & thePath)

Actually since I see you want to exclude the .DS_Store and .localized from your other thread might I suggest this then instead?

set thePath to quoted form of POSIX path of (path to desktop folder)
set allList to paragraphs of (do shell script "/bin/ls -A " & thePath & " | grep -v .DS_Store | grep -v .localized")

That’s better than system events James! Here’s my speed tests of it versus ‘list folder’, with list folder being the first time. These times are for 500 iterations. The speed test can be found here in post #4 by Nigel Garvey here: http://bbs.applescript.net/viewtopic.php?id=17015.

Unfortunately it’s still 18 times slower…
First Test Time: 0.426
Second Test Time: 7.617
Times Faster: 17.9

/ponder

Well, not sure what to say then. I’m guessing that has to do with the shell call itself because I can’t see the ‘ls’ being any slower itself.

On my machine, a shell call has an overhead of about 100 ms.

And because I’m bored here’s one to only give you file names not folder names

set thePath to quoted form of POSIX path of (path to desktop folder)
set allList to paragraphs of (do shell script "find " & thePath & " -type f -maxdepth 1 | grep -v .DS_Store | grep -v .localized | sed 's:" & thePath & "/::'")

My machine isn’t too different from yours Adam, dual 2GHz G5… so that doesn’t make sense. If my math is right, then 10 shell calls would equal 1 second. I made 500 calls which would equate to 50 seconds.

10 ms sounds more correct than 100 ms.

Right – that’s an old number. Here’s the test (don’t ask me to explain the perl script, I found it)

set ProcTime to "perl -e 'use Time::HiRes qw(time); print time'"
set t1 to 0
set t2 to 0
set N to current date
set SysTime0 to N - (do shell script ProcTime) - (time to GMT)
repeat 10 times -- get the pumps primed
	do shell script ProcTime
end repeat
repeat 100 times -- do the test for no shell call (the latency in doing a shell script??)
	set t to do shell script ProcTime
	set t1 to t1 + (do shell script ProcTime) - t
end repeat
repeat 100 times
	set t to do shell script ProcTime
	do shell script "ls -AFR ~/Pictures/"
	set t2 to t2 + (do shell script ProcTime) - t
end repeat
{t1 / 100, t2 / 100, (t2 - t1) / 100}

I get exactly the same result, but one-and-a-half to two times as quickly, simply by omitting the -A parameter. But I’m not an expert on shell scripts. Is there any subtle difference between what the two versions do?

set thePath to quoted form of POSIX path of (path to desktop)
set allList to paragraphs of (do shell script "/bin/ls " & thePath)

I ran up against regulus’s dilemma the other day while working on something of my own. My reactionary reaction was to keep using ‘list folder’ for as long as possible: :wink:

set theAlias to (path to desktop)
try
	set allList to (list folder theAlias without invisibles)
on error
	-- When 'list folder' eventually stops working, this, the second fastest method, should kick in.
	-- The time taken to recover from the error will handcap its speed, but computers capable of
	-- running the systems where it'll be necessary will be faster anyway..
	-- NB. 'list folder' returns names sorted case-insensitively, 'ls' case-sensitively.
	set allList to paragraphs of (do shell script "/bin/ls " & quoted form of POSIX path of theAlias)
end try

‘ls’ just on its own, unless you have it aliased otherwise, will not return system files (those starting with a .). So the two options then are to use ls -a or ls -A. The small ‘-a’ will return those invisibles along with the . and … entries whereas the large ‘-A’ will exclude them. Just for giggles I wonder if this might be faster somehow

set thePath to quoted form of POSIX path of (path to desktop folder)
set allList to paragraphs 3 thru -1 of (do shell script "/bin/ls -a " & thePath & " | grep -v .DS_Store | grep -v .localized")

For 500 iterations, the times were within 0.1 secs of each other… so they’re identical.

That’s what I decided too! The time difference is just too much to not use it. There’s a noticeable difference in my application between the two methods.