-2

I'm having some problems working in the files in an ordered way using Visual Basic. Basically I'm reading all files in a folder that are named as 1.jpg, 2,jpg, .... 10.jpg, 11.jpg.

When I'm creating my logic I need those files are worked in ascending order by the file name (1,2,3...,8,9,10,11). Actually with my codes the files are being worked as a string way I guess (1,10,2,20,3,4,5).

Can someone help me in this case since I can't get it working? Tried two different codes but can't reach the solution.

I've tried with:

Dim MyFiles() As String = IO.Directory.GetFiles(folder)
For Each file In myfiles

And also tried with:

Dim myfiles As FileInfo()
myfiles = New DirectoryInfo(folder).GetFiles()
For Each file In myfiles

Can someone help me please?

Trevor
  • 7,777
  • 6
  • 31
  • 50
Tiago
  • 625
  • 5
  • 16
  • Type a . after myfiles and you will notice OrderBy() just order by that what you need, in your case myfiles.OrderBy(f=>f.Name); just make sure you hav e system.linq namespace – Walter Verhoeven Aug 26 '18 at 12:43
  • @ComputerAidedTradingSystems, that won't work. The paths are already sorted by name. The problem is that they are sorted alphabetically rather than numerically. You have to convert the names to numbers to get numeric sorting. – jmcilhinney Aug 26 '18 at 13:01
  • 2
    Possible duplicate of [Sorting Directory.GetFiles()](https://stackoverflow.com/questions/52842/sorting-directory-getfiles), just pick an answer. This form of question in some form or another is all over SO, we don't need another one. We should be voting to close dups we find, not promote them IMHO. – Trevor Aug 26 '18 at 13:59

1 Answers1

1

If your file names are just numbers then it's easy:

Dim MyFiles() As String = IO.Directory.GetFiles(folder)

For Each file In myfiles.OrderBy(Function(filePath) CInt(IO.Path.GetFileNameWithoutExtension(filePath)))

If your file names contain text and numbers then it's a little more complex but there's a Windows API function that can be used to order file names the way File Explorer does, because File Explorer uses that function. If you need that, leave a comment and I'll post the specifics.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • 2
    Also if you're on .NET 4.0 or higher, take advantage of `Directory.EnumerateFiles()` – Parrish Husband Aug 26 '18 at 14:21
  • 1
    @ParrishHusband, there are times where `GetFiles` is still preferable, e.g. if you want an array to bind to a `ComboBox`. That said, based on the code provided in the question, i.e. using a `For Each` loop on the results, `EnumerateFiles` would likely be preferable in this case. – jmcilhinney Aug 26 '18 at 14:38
  • yeah that makes sense. Would there be benefits to `Directory.GetFiles()` vs `Directory.EnumerateFiles().ToArray()` in cases like that? I'd assume they'd perform the same right? – Parrish Husband Aug 26 '18 at 14:42
  • 1
    @ParrishHusband, I would expect `GetFiles` to perform better but by such a small amount as to not matter from that perspective. There is an overhead associated with things like iterators but you'd almost certainly not notice it for a single call. There are also obvious advantages to iterators too but you can't take advantage of them if you immediately call `ToArray`. – jmcilhinney Aug 26 '18 at 15:21
  • 1
    There's an example of using StrCmpLogicalW at [How to order list of files by file name with number?](https://stackoverflow.com/a/33786276/1115360). – Andrew Morton Aug 26 '18 at 15:45