Answered by:
Directory.GetFiles does not work as documented...

Question
-
I am trying to use GetFiles to get a list (array) of files on particular drives, however in some cases I do not get what I expect at all. For example:
Dim ScanFiles AsIEnumerable(OfString) = Enumerable.Empty(OfString)()
ScanFiles = Directory.GetFiles("C:\", "*.*", SearchOption.AllDirectories)
I would expect this to return all files on drive C, however I only get 49 files and it seems to cutoff at a folder called \Config.msi. No matter how I have tried to tweak this, it will not return the expected files. (Note - there is NO password protection on this drive or any folder on the drive.
As well, I use this command (much as it appears above but with a different drive letter) and it NEVER lists the files in the root. This is especially frustrating since mapped drives are used quite a bit in the project I am trying to complete, and most of those drives have files in the root. It never picks them up.
So why is there an option that says "AllDirectories" if in fact you don't get all directories. Is there any alternative to this that I can use?
Thanks
- Moved by Jack Zhai-MSFTMicrosoft contingent staff Tuesday, January 28, 2014 4:44 AM Move to a more appropriate forum.
Friday, January 24, 2014 6:37 PM
Answers
-
See if the code below works. I used enumerators so when a credential issue fails the code continues.
Imports System.IO Module Module1 Dim ScanFiles As List(Of String) = New List(Of String) Sub Main() ScanFiles = GetSubFolder("C:\", "*.*") End Sub Function GetSubFolder(folder As String, filePattern As String) As List(Of String) Dim successful1 As Boolean = True Dim successful2 As Boolean = True Dim files As List(Of String) = New List(Of String) Dim subFolders As List(Of String) = New List(Of String) Dim enumerator As IEnumerator(Of String) = Nothing Try enumerator = Directory.GetDirectories(folder).AsEnumerable().GetEnumerator() While (True) Try If (Not enumerator.MoveNext()) Then Exit While End If subFolders.Add(enumerator.Current) Catch ex As Exception Dim errMsg As String = String.Format("Error Cannot get folder '{0}' : {1}", folder, ex.Message) Console.WriteLine(errMsg) successful1 = False End Try End While If successful1 = True Then For Each subFolder As String In subFolders files.AddRange(GetSubFolder(subFolder, filePattern)) Next subFolder enumerator = Directory.GetFiles(folder, filePattern).AsEnumerable().GetEnumerator() While (True) Try If (Not enumerator.MoveNext()) Then Exit While End If files.Add(enumerator.Current) Catch ex As Exception End Try End While End If Return files Catch ex As Exception Dim errMsg As String = String.Format("Error Cannot get folder '{0}' : {1}", folder, ex.Message) Console.WriteLine(errMsg) successful1 = False End Try End Function End Module
jdweng
- Marked as answer by B_E_L Tuesday, January 28, 2014 2:51 PM
Sunday, January 26, 2014 12:16 PM
All replies
-
Probably you do not have rights to access the “Config.msi” folder. If you use 'On Error Resume Next', then try again without this statement, and see if exceptions give some details.
If the problem is caused by access rights, then probably you have to write a larger recursive program, ignoring denied files and folders.
Sunday, January 26, 2014 10:37 AM -
See if the code below works. I used enumerators so when a credential issue fails the code continues.
Imports System.IO Module Module1 Dim ScanFiles As List(Of String) = New List(Of String) Sub Main() ScanFiles = GetSubFolder("C:\", "*.*") End Sub Function GetSubFolder(folder As String, filePattern As String) As List(Of String) Dim successful1 As Boolean = True Dim successful2 As Boolean = True Dim files As List(Of String) = New List(Of String) Dim subFolders As List(Of String) = New List(Of String) Dim enumerator As IEnumerator(Of String) = Nothing Try enumerator = Directory.GetDirectories(folder).AsEnumerable().GetEnumerator() While (True) Try If (Not enumerator.MoveNext()) Then Exit While End If subFolders.Add(enumerator.Current) Catch ex As Exception Dim errMsg As String = String.Format("Error Cannot get folder '{0}' : {1}", folder, ex.Message) Console.WriteLine(errMsg) successful1 = False End Try End While If successful1 = True Then For Each subFolder As String In subFolders files.AddRange(GetSubFolder(subFolder, filePattern)) Next subFolder enumerator = Directory.GetFiles(folder, filePattern).AsEnumerable().GetEnumerator() While (True) Try If (Not enumerator.MoveNext()) Then Exit While End If files.Add(enumerator.Current) Catch ex As Exception End Try End While End If Return files Catch ex As Exception Dim errMsg As String = String.Format("Error Cannot get folder '{0}' : {1}", folder, ex.Message) Console.WriteLine(errMsg) successful1 = False End Try End Function End Module
jdweng
- Marked as answer by B_E_L Tuesday, January 28, 2014 2:51 PM
Sunday, January 26, 2014 12:16 PM -
Thank you both for your replies. I will try to migrate this idea and code into what I am doing to see if I can obtain what I am after.
Thanks again!
Tuesday, January 28, 2014 2:51 PM