Error in System.IO.Directory.GetFiles?

  • Thread starter Thread starter Mobile Boy
  • Start date Start date
M

Mobile Boy

Hello group,



When using "*.doc" as searchpattern, Getfiles is filling the enumerator with
doc-files but also incorrectly with docx files.
Try this little piece of code to reproduce this issue.
Why does "*.doc" ends in a list of both, doc-files and docx-files?



Dim En As IEnumerator

Dim FoundFile As String

En = System.IO.Directory.GetFiles(_Directory, "*.doc").GetEnumerator()

While En.MoveNext

FoundFile = System.Convert.ToString(En.Current.ToString)



End While





Best regards,

Mobile boy
 
Mobile Boy wrote on 1/9/2013 :
Hello group,



When using "*.doc" as searchpattern, Getfiles is filling the
enumerator with doc-files but also incorrectly with docx files.
Try this little piece of code to reproduce this issue.
Why does "*.doc" ends in a list of both, doc-files and docx-files?



Dim En As IEnumerator

Dim FoundFile As String

En = System.IO.Directory.GetFiles(_Directory,
"*.doc").GetEnumerator()

While En.MoveNext

FoundFile = System.Convert.ToString(En.Current.ToString)



End While





Best regards,

Mobile boy

Read the documentation -
http://msdn.microsoft.com/en-US/library/wz42302f(v=vs.110).aspx

In the notes:

<quote>
A searchPattern with a file extension of exactly three characters
returns files having an extension of three or more characters, where
the first three characters match the file extension specified in the
searchPattern.
</quote>

In other words, it's by design.
 
OK, Thank you Tom for your answer.
So you have to build in an extra check to see wetherthe extension is doc or
docx. Good to know!

Thanks.
 
Back
Top