-2

I am trying to get files from system in my application. But when retrieving files it used to come but not in sorted order. I want all my files to come in sorted order. I am very new to this visual basic and i have tried so many things but unable to get files in sorted order. Please Help me out, thanks in advance.

Here is my code : -

 Dim path As String = "C:/UTRelOFA/Reva Test/src/00000178"
    Dim oFolder As New DirectoryInfo(path)

   Dim arr =  oFolder.GetFiles(".tif")
     Array.Sort(arr)
    Cosole.writeline(arr)

I have used so many things and refered this link : Sorting Directory.GetFiles()

Ashish yadav
  • 117
  • 2
  • 11
  • 3
    You're calling `Array.Sort` on the array returned by one call to `GetFiles` but then you just discard that array and call `GetFiles` again and use that unsorted result. You need to actually assign the result of `GetFiles` to your `arr` variable, then pass `arr` to `Array.Sort`. Also, don't specify a size for `arr` when you declare it. You don't create the array. `GetFiles` creates it and the size is determined by the number of files it gets. – jmcilhinney Aug 17 '18 at 10:51
  • arr = oFolder.GetFiles(".tif") and pass it to Array.Sort(arr) are you talking about this . I have tried this but this is not working @jmcilhinney – Ashish yadav Aug 17 '18 at 10:55
  • Can you provide me proper code for this problem it will be helpful for me – Ashish yadav Aug 17 '18 at 10:56
  • How about you follow the instructions provided and then, if it doesn't work, edit your question and post THAT code and tell us what it actually did do and how it differed from what you expect? If it didn't work then you did it wrong so we need to see what you did. – jmcilhinney Aug 17 '18 at 11:00
  • An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll. It use to give this error on line Array.sort(arr) @jmcilhinney – Ashish yadav Aug 17 '18 at 11:11
  • I have edited my code you can see @jmcilhinney – Ashish yadav Aug 17 '18 at 11:14
  • I told you not to specify a length when you declared the `arr` variable but you still need to actually declare it as a an array type. As it is, you're declaring it as just a `FileInfo` rather than a `FileInfo` array. – jmcilhinney Aug 17 '18 at 11:15
  • 1
    You don't even need to specify a type anyway. You can just do this use `Dim arr = oFolder.GetFiles("*.tif")` and the type of the variable will be inferred from the return type of the method. Also notice that the search pattern needs to be "*.tif" rather than ".tif". Your pattern won't match anything because there are no files with that exact name. – jmcilhinney Aug 17 '18 at 11:17
  • 1
    Also, please turn `Option Strict On` in the project properties. That will help catch issues related to incorrect data types at compile time instead of at run time. – jmcilhinney Aug 17 '18 at 11:18
  • same error exists. Invalid operationException @jmcilhinney – Ashish yadav Aug 17 '18 at 11:28
  • Why do I have to say this yet again? If you have changed your code then edit your question and show us what you're doing now because we can't just guess what you're doing wrong. – jmcilhinney Aug 17 '18 at 12:37
  • Check edited code @jmcilhinney – Ashish yadav Aug 17 '18 at 12:51
  • Can you provide examples of the filenames and how they are not sorted? Maybe they are sorted but not as you expect. – UnhandledExcepSean Aug 17 '18 at 13:09
  • There are still some issues with your code but it just dawned on me that you're not calling `Directory.GetFiles` so the result is not simply a `String` array so you won't get the `FileInfo` array sorted as though they were. I'll post an answer. – jmcilhinney Aug 17 '18 at 13:15

1 Answers1

2

The question here is whether you should be calling DirectoryInfo.GetFiles or Directory.GetFiles. If you call the former then you get a FileInfo array, so if you want to sort by file name then you have to specify that explicitly, e.g.

Dim folderPath = "C:\UTRelOFA\Reva Test\src\00000178"
Dim folder As New DirectoryInfo(folderPath)
Dim files = folder.GetFiles("*.tif")

Array.Sort(files, Function(fi1, fi2) fi1.Name.CompareTo(fi2.Name))

If you don't actually need FileInfo objects but rather just the file paths, don;t use a DirectoryInfo, e.g.

Dim folderPath = "C:\UTRelOFA\Reva Test\src\00000178"
Dim filePaths = Directory.GetFiles(folderPath, "*.tif")

Array.Sort(filePaths)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • As an alternative to Array.Sort, I am more comfortable with a little Linq. `Dim sortedFiles = From file In files Select file Order By file.Name` – Mary Aug 18 '18 at 04:01
  • I thought about adding the LINQ alternative but I figured I'd keep it simple. If I was going to use LINQ, I'd tend to use function syntax, i.e. `Dim sortedFiles = files.OrderBy(Function(fi) fi.Name).ToArray()`. Note that, using query syntax or function syntax, the `ToArray` or `ToList` call is pretty much required unless all you intend is to run a single `For Each` loop over the result. – jmcilhinney Aug 18 '18 at 04:09
  • Since I am always using .ToArray or .ToList I forgot about that point. The weird (to me) data type returned from the unadorned Linq query makes me always use something I am more familiar with. Thanks for the reminder. – Mary Aug 18 '18 at 04:59
  • @Mary, you can consider a LINQ query to simply return an `IEnumerable(Of T)`. It will generally be a more specific type than that, depending on what extension method was called last, but the fact that it is an `IEnumerable(Of T)` is pretty much all that matters. That means that if you just want to enumerate it with a `For Each` then you can use it as is but if you want random access to the items then you should call `ToArray` or `ToList`. – jmcilhinney Aug 18 '18 at 05:34
  • Thank you again @jmcilhinney . An explanation that even I can understand! An interface and a generic in one return value was enough to scare me. :-) Saved to my reference file. BTW, "run a single For Each loop over the result" I tried running a second For Each and it still worked. Sorry for the nit-pick but I am married to an engineer. :-) – Mary Aug 18 '18 at 06:10
  • @Mary, yes, you can enumerate an `IEnumerable(Of T)` as many times as you like but that can become a problem with the result of a LINQ query. That's because LINQ queries use deferred execution, which means that the query gets executed every time you enumerate it. That's why you should call `ToArray` or `ToList` if you want random access: to avoid multiple executions of the query. Random access includes multiple enumerations. – jmcilhinney Aug 18 '18 at 06:27
  • @Now it is really starting to make sense. I have working my way through C# Pro and I remember reading about that but without much understanding. So my fear of IEnumerable lead me to a safer and quicker implementation with .ToArray and .ToList. Got it. – Mary Aug 18 '18 at 07:14