Solved

Need help to pull random files from a folder using VB.NET

Posted on 2013-01-29
7
328 Views
Last Modified: 2013-01-29
Hi all, I have a procedure in .NET that builds a list of files in a folder that is used to build a datatable containing image name and path.  The datatable gets bound to a datalist that is used by jquery & lightbox.  The code works as expected.  The folder in question will continue to grow and hold hundreds of files. What I would like to do is alter the code so it selects 12 or so random images each time it runs but have no clue how to use the random().  Code is below and any help would be great!
 Private Sub BindList()
       
        Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning"))
        Dim imageNames = New String(imagePaths.Length - 1) {}
        For i As Integer = 0 To imagePaths.Length - 1
            imageNames(i) = imagePaths(i).Substring(imagePaths(i).LastIndexOf("\") + 1)
        Next

        Dim dt = New DataTable()
        dt.Columns.Add("ImageName", GetType(String))
        dt.Columns.Add("ImagePath", GetType(String))

        'select random number of files from imageNames but how?
            For Each imgName In imageNames
                Dim dr As DataRow = dt.NewRow()
                dr("ImageName") = RemoveExtension(imgName)
                dr("ImagePath") = "Images/Burning/" & imgName
                dt.Rows.Add(dr)
            Next


        DataList1.DataSource = dt
        DataList1.DataBind()
    End Sub
0
Comment
Question by:MVaughn12
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38832785
You can change

For i As Integer = 0 To imagePaths.Length - 1

to


Dim Count As Integer = IIF(imagePaths.Length > 11, 11, imagePaths.Length - 1)
For i As Integer = 0 To Count
0
 

Author Comment

by:MVaughn12
ID: 38832801
But won't that just grab the 12 images?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38832808
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38832900
You could do:
    Private Sub BindList()
        Dim imagePaths() As String = Directory.GetFiles(Server.MapPath("Images/Burning"))
        Dim imageNames As New List(Of String)
        For Each Path As String In imageNames
            imageNames.Add(Path.Substring(Path.LastIndexOf("\") + 1))
        Next
        Static R As New Random
        While imageNames.Count > 12
            imageNames.RemoveAt(R.Next(imageNames.Count))
        End While

        Dim dt = New DataTable()
        dt.Columns.Add("ImageName", GetType(String))
        dt.Columns.Add("ImagePath", GetType(String))

        For Each imgName In imageNames
            Dim dr As DataRow = dt.NewRow()
            dr("ImageName") = RemoveExtension(imgName)
            dr("ImagePath") = "Images/Burning/" & imgName
            dt.Rows.Add(dr)
        Next

        DataList1.DataSource = dt
        DataList1.DataBind()
    End Sub

Open in new window

0
 

Author Comment

by:MVaughn12
ID: 38832940
No errors, but now I have no images showing on the page. Even if i set the > to someting like 3 which i assume would give me 3 images?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 38832952
Sorry!...

Change line #4:

    For Each Path As String In imageNames

To this:

    For Each Path As String In imagePaths
0
 

Author Closing Comment

by:MVaughn12
ID: 38832969
Thanks this is just what I was looking for.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now