Solved

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

Posted on 2013-01-29
7
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

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 86

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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