2

I want to know if there is a way to check if a folder contains subfolders, but without using the FindFirst/FindNext to check every item, because it's slow when the folder contains many files and no subfolders.

Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55
  • 1
    You have to enumerate the contents until you find a subfolder – David Heffernan Nov 23 '14 at 14:24
  • I'd believe that it's *slower*, but not that it's actually *slow*. That makes me suspect you're doing something wrong. – Rob Kennedy Nov 23 '14 at 14:42
  • [`FindFirstFileEx`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364419%28v=vs.85%29.aspx) with `fSearchOp` parameter set to [`FindExSearchLimitToDirectories`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364416%28v=vs.85%29.aspx#FindExSearchLimitToDirectories) might be faster. – TLama Nov 23 '14 at 14:53
  • 1
    .. and [unreliable](http://stackoverflow.com/questions/2248911/file-system-support-for-findfirstfileex-limit-to-directories). – Sertac Akyuz Nov 23 '14 at 14:56
  • Time this from a CMD prompt at the root of a drive: "dir `*.*` /s > dump.txt". Doing the equivalent thing in Delphi code using FindFirst/FindNext takes a few percent longer, 11 secs for ~250,000 files here. – MartynA Nov 23 '14 at 17:40
  • @MartynA It might be interesting to compare that with `dir /ad/s` to determine if the OS uses a more efficient way of identifying subfolders. Though the output should be redirected elsewhere to avoid contaminating the benchmark. – Disillusioned Nov 24 '14 at 17:19

1 Answers1

1

I think in general there is no way to ask a file system object if it contains any directories. I think you need to enumerate the object's children as you have described, for instance with calls to FindFirstFile, FindNextFile etc.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 4
    Why not give a try to the `FindFirstFileEx` function with `FindExSearchLimitToDirectories` flag ? If the file system won't support directory filtering (and the call returns a non-directory object data), you'll just loop with the `FindNextFile` until you find a directory object. My point is that you can increase the speed by the chance that the file system supports that filtering and you'll find a directory with a single call in such case. – TLama Nov 23 '14 at 21:52
  • The supported file systems are known ? Is there a list somewhere ? I didn't find anything on google... – Marus Gradinaru Nov 24 '14 at 12:20