Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-29
7
Medium Priority
?
349 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

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