How to find the location of file under a directory tree?

Given a filename, say foo.txt and a base directory URL, say ~/bar/directory what's the way to find the subdirectory for the first occurrence of the file in a BFS manner?


i.e. given a directory structure of


/bar

/directory

/subdirectory

bar.txt

foo.txt


Match foo.txt before going down the /directory path.

The only API for file traversal I have found does DFS, see `NSFileManager.enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:.

Look here how to get the colmplete list of files and directories inside a directory.


You could filter on filename to get the list and select the first in the list


h ttps://stackoverflow.com/questions/27721418/getting-list-of-files-in-documents-folder

AFAICS this assumes you know what directory has the file.

This is a case where you have a base directory to start traversing from but don't know on which subdirectory the file is located.

That's what I understood you meant with base directory. But maybe I was wrong ?

The question is about having a base directory, which acts as the root to start the search from.


In the link you have shared, AFAICS, the answers talk about FileManager.contentsOfDirectory which "Performs a shallow search of the specified directory".

Given that your search is not well-defined anyway, I don't think it's unreasonable that you should have to write your own algorithm for descending through the directory hierarchy.


Why is not well-defined? Because:


— You don't specify which of two files, on different branches but at the same depth, you want to use.


— You don't control the order in which items in a single subdirectory are enumerated.


Note that the enumeration is likely neither depth-first nor breadth-first. There is no API contract about the order in which files and subdirectories are traversed.

Not sure I understand why you say it's not well defined.


What do you mean by different branches on the same depth? Can you give an example?


Regarding the order in which items in a single subdirectory are enumerated. I don't think that's an issue, as long as directories are not traversed before exhausting the search in the directory being searched.


FWIW, here is what Philippe Hausler had to say:


@qnoid IIRC yes, perhaps it should have options for that? Maybe a good enhancement request

ref: https://twitter.com/phausler/status/984111194309967872

Hello qnoid,

Use NSMetadataQuery and let Spotlight do the work for you.

>> What do you mean by different branches on the same depth? Can you give an example?


Sure. Suppose you have this hierarchy:


/bar
     /directory
          /subdirectory1
               foo.txt
          /subdirectory2
               foo.txt


Unless you have a guarantee about the order in which directory's contents searched, you don't know which foo.txt you'll find. Or, slightly more suble, suppose you have this hierarchy:


/bar
     /directory
          /subdirectory1
               /subsubdirectory
                    foo.txt
          /subdirectory2
               foo.txt


If subdirectory1 happens to be searched before subdirectory2, I'd expect it would recurse into subsubdirectory and find foo.txt deeper in the hierarchy.


Presumably you have specific wishes for the outcome in the various cases. The only way you can be certain you get the result you want is to manage every step of the search.

How to find the location of file under a directory tree?
 
 
Q