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
Solved

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

Posted on 2013-01-29
7
335 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
vb.net class 3 21
Help with preventing selection from a combobox 11 34
Generate Numbers in JQuery file 11 66
run a stored procedure from vb.net 1 27
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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