# DirectoryIterator bug

**URL:** <https://forum.juce.com/t/directoryiterator-bug/11298>\
**Category:** General JUCE discussion\
**Created:** [August 17, 2013, 2:14pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298 "2013-08-17T14:14:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [August 17, 2013, 2:14pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/1 "2013-08-17T14:14:32Z")

</div>

The **DirectoryIterator** class has a bug that prevents regular files to be filtered against a file mask.

The **DirectoryIterator::next** function contains the following code:

```
// if recursive, we're not relying on the OS iterator to do the wildcard match, so do it now..
if (matches && **isRecursive** )
    matches = fileMatches (wildCards, filename);
```

...that should be changed to:

```
// if recursive, we're not relying on the OS iterator to do the wildcard match, so do it now..
if (matches &&**((isDirectory && isRecursive) || !isDirectory)**)
    matches = fileMatches (wildCards, filename);
```

&nbsp;

I hope this fix is&nbsp;useful, Julian.

Best regards.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 17, 2013, 9:07pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/2 "2013-08-17T21:07:12Z")

</div>

No.. I think you've misunderstood that. If the item is not a directory, then there's no need to call fileMatches(), because the match will already have been done by the OS-specific file iterator. If there's a bug, maybe give me an example of some code that I can use to reproduce it, and I'll take a look?

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [August 18, 2013, 7:14pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/3 "2013-08-18T19:14:00Z")

</div>

Here is the code that reproduces the problem:

```
int main (int argc, char* argv[])
{
    File baseDir("C:/Temp/Test");

    DirectoryIterator di(baseDir, false, String("*.jpg;*.png;*.gif"));

    while (di.next())
        std::cout << di.getFile().getFullPathName() << std::endl;

    return 0;
}
```

...and the partial output of its execution is:

```
...
C:\Temp\Test\aldus-logo.tif
C:\Temp\Test\cute-puppy-dog-wallpapers_0.jpg
C:\Temp\Test\Desktop.ini
C:\Temp\Test\dogs_100.jpg
...
```

Do you see the "aldus-logo.tif" and "Desktop.ini" files?

------------------------------------

P.S. OS Windows 8 /&nbsp;64-bit

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 19, 2013, 9:39am UTC](https://forum.juce.com/t/directoryiterator-bug/11298/4 "2013-08-19T09:39:05Z")

</div>

Thanks - looks like the problem was caused&nbsp;when it had multiple wildcards.&nbsp;Try it again now..

---

<div class="post-metadata">

**Author:** ![ptomaine](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ptomaine/32/245_2.png) [@ptomaine](https://forum.juce.com/u/ptomaine)\
**Post date:** [August 19, 2013, 5:20pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/5 "2013-08-19T17:20:39Z")

</div>

Thank you, Julian! It works!

---

<div class="post-metadata">

**Author:** ![fireplayer](https://avatars.discourse-cdn.com/v4/letter/f/48db29/32.png) [@fireplayer](https://forum.juce.com/u/fireplayer)\
**Post date:** [March 28, 2014, 2:52pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/6 "2014-03-28T14:52:00Z")

</div>

In OSX if you choose \*.\* as your filter DirectoryIterator::next() ignores all files. This does not happen on Windows. I see you have it trapped in&nbsp;WildcardFileFilter::parse but not in&nbsp;DirectoryIterator::next().

BTW, I have not checked this against the latest tip.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 28, 2014, 9:23pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/7 "2014-03-28T21:23:10Z")

</div>

It's not really a bug. I vaguely remember reluctantly adding&nbsp;the hack in WildcardFileFilter for reasons that I forget (something to do with file choosers, I think...),&nbsp;but you should never actually&nbsp;use \*.\* yourself - always use \* if you want&nbsp;all the files.

And if you're making this request because&nbsp;you think your users might enter \*.\* somewhere, then it's the job of your own app to detect and&nbsp;replace that string -&nbsp;it's not a policy that should be hard-coded into a fundamental class like DirectoryIterator. (If I did that, how would someone write an algorithm that searches for only those filenames which contain a dot!?)

---

<div class="post-metadata">

**Author:** ![fireplayer](https://avatars.discourse-cdn.com/v4/letter/f/48db29/32.png) [@fireplayer](https://forum.juce.com/u/fireplayer)\
**Post date:** [March 28, 2014, 11:29pm UTC](https://forum.juce.com/t/directoryiterator-bug/11298/8 "2014-03-28T23:29:09Z")

</div>

That is what I did, just like your hack and it seems to work. thankfully I don't have users entering wild card fields.
