How can I list the files in a folder in physical order in Visual Basic?

53 Views Asked by At

In VB.NET (Visual Basic) I would like to list the files in a Windows folder in the physical order that they were stored on the disc. I have some mp3 players that do not sort the songs in alphabetic order, but just play them in the order that they were stored in the disc. Example:

I copy the files to a folder in this order and the mp3 player plays them in the same order:

04 Song 4
07 Song 7
01 Song 1
06 Song 6
02 Song 2

I am not talking about a Visual Basic program playing the songs. My Juke Box program works fine. I am talking about a little radio/mp3 player, and the infotainment system in one of my cars. I would like to use Visual Basic to show me the order they will be played in, to tell me if I need to re-store all of them or if the folder will play correctly. If I use a DOS command prompt and a DIR command, it shows them in their physical order. I want to be able to do that with Visual Basic.

I do not want to run a DOS command. It would be nice to get the file names and directory info into the program.

Googling has provided me with no help.

I have looked at the parameters on the FileIO.FileSystem.GetFiles(...) command and see nothing of any help.

1

There are 1 best solutions below

2
Albert D. Kallal On

Ok, for a simple example, you can pull files from a folder like this:

Private Sub ListBoxPicture_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim sPicturesDir = "c:\test\balls"
    Dim f As New DirectoryInfo(sPicturesDir)
    Dim fFiles = f.GetFiles("*.png")

    For Each fL As FileInfo In fFiles
        ListBox1.Items.Add(fL.Name)
    Next

End Sub

Now, in above I used *.png, but you could of course use *.mp3

So, I created a form with a listbox, and the result is now this:

enter image description here

And the order is the same as the files in the folder

Say this:

enter image description here

And if you wish, you can show the full file name with this code:

        ListBox1.Items.Add(fL.FullName)

And the result is now this:

enter image description here

And since we using "FileInfo", then the size of the file is also available.

Say this:

        ListBox1.Items.Add(fL.Name & " (" & fL.Length & ")")

And we now have this:

enter image description here

So, there is no sorting or changing order of the files in that folder, and as above shows, files are returned in the order they have in that folder.