Solved

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

Posted on 2013-01-29
7
334 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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