Directory listing

Hi All,

I am trying to get a folder list of a parent folder without its sub-folders. I am however unable to accomplishes this without getting all the folder being enumerated.

Is there a way to do this without having to use appleScipt code to filter out sub-folders.

Thank you in advance.

Here is my code


set paDir to (choose folder with prompt "Choose a folder to scan")

set folderPath to current application's NSString's stringWithString_(POSIX path of paDir)

set fileManager to current application's NSFileManager's defaultManager()
set enumerator to fileManager's enumeratorAtPath_(folderPath)

set theList to {}
repeat
	set nextDir to enumerator's nextObject()
	--log nextDir
	if nextDir is missing value then exit repeat
	-- Inset code here
	
	set end of theList to nextDir
end repeat
log theList

Do you just want the folders of your parent folder, or all the top level objects, files included? The methods, contentsOfDirectoryAtPath:error: and contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:, both do shallow searches – that is, they don’t look at the contents of any sub folders. There’s a subject heading, “Discovering Directory Contents”, in the NSFileManager Class reference, you should check out each of those methods and see what each one does for you.

Incidentally, there’s no need for your loop to get an array or applescript list from the directory enumerator – NSDirectoryEnumerator’s superclass, NSEnumerator, has a method allObjects() the returns an array of all the enumerator’s objects.

Ric

Thanks for your reply Ric. I tried the contentsOfDirectoryAtPath:error: method but it returned files as well as hidden files. I also tried contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: method but without hidden files. I am trying to get list of folders only.

Cheers
t


set paDir to (choose folder with prompt “Choose a folder to scan”)

set folderPath to current application’s NSString’s stringWithString_(POSIX path of paDir)

set fileManager to current application’s NSFileManager’s defaultManager()
–set enumerator to fileManager’s enumeratorAtPath_(folderPath)
set folderList to fileManager’s contentsOfDirectoryAtPath_error_(folderPath, missing value)

There is no method in cocoa to return just folders – the ones I mentioned do the shallow search, but they return both files and folders. This is kind of a pain, I’ve been wanting methods that would only return either folders or files, but not both. Right now I am writing a category on NSFileManager to do just that. I’ll post it when I’ve finished.

Ric