Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > Random File Chooser


Reply
 
Thread Tools Display Modes
  #1  
Old 04-17-2009, 12:19 PM
Jazman667's Avatar
Jazman667 Jazman667 is offline
Freshman
 
Join Date: Apr 2009
Posts: 26
Default Random File Chooser


I am a total noob to VB and am only doing an A-level in computing. i'm using VB 2008 express edition

i am trying to make a windows form application which picks a random file from a folder and displays its contents in text boxes when a button is clicked. i have tried everything my feeble A-level mind can Think of and need some help.

Any suggestions?
Reply With Quote
  #2  
Old 04-17-2009, 12:43 PM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
 
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
Default

Firstly, this is in the wrong forum... you need to post in .NET although I imagine this will be moved for you...

You have 3 problems:
1) Enumerate files in a directory
2) Pick one at random (by generating a random number between 1 and the number of files found in the directory)
3) Read the file into a string and print into a textbox.

I recommend you tackle these problems in the order I've stated above. For problem 1, something along the lines of...
Code:
Dim ioDirectory As New IO.DirectoryInfo("c:\path\to\your\directory")
Dim ioFiles As IO.FileInfo() = Directory.GetFiles("*.*") 'or maybe *.txt for text files
Dim ioFile As IO.FileInfo

For Each ioFile In ioFiles
   MsgBox(ioFile.Name)
Next
... should get you started.
__________________
Regards
John, jlsd.co.uk
Reply With Quote
  #3  
Old 04-17-2009, 12:56 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

First. Welcome to the forum. Don't forget to read the forum Posting Guidelines.

Use the System.IO.Directory and System.Random objects.

Code:
Public Class Form1 Dim myFiles() As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Run this once. myFiles = System.IO.Directory.GetFiles("C:\", "*.*") ' Apply your own folder path End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim R As New System.Random Dim index As Integer ' Point to different elements of the myfiles array index = R.Next(myFiles.Length - 1) MessageBox.Show(myFiles(index)) End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #4  
Old 04-17-2009, 01:59 PM
Jazman667's Avatar
Jazman667 Jazman667 is offline
Freshman
 
Join Date: Apr 2009
Posts: 26
Default Cheersen

Cheers Gruff

it seems to work fine if i just put the button 1 code in the button 2 sub, why did you do that in the first place? (trying to broaden my knowledge here not just randomly questioning you lol)
Reply With Quote
  #5  
Old 04-17-2009, 03:31 PM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
Default

Quote:
Originally Posted by Jazman667 View Post
Cheers Gruff

it seems to work fine if i just put the button 1 code in the button 2 sub, why did you do that in the first place? (trying to broaden my knowledge here not just randomly questioning you lol)
Gruff seperated the functionality of gathering information about which files was located in the specified folder and the actually randomly selecting one among the found files. If you think about it, it should make perfectly good sense performance wise assuming that no new files are getting added to the folder.

Imagine having a folder with 1 million files. If you had to create a list of all the files everytime you wanted to select a random file, then you'd have to
  1. Create a new list
  2. Iterate all 1 million files adding them to the list
  3. Select random file from list
Instead, if you just maintained the list of files, then you could simply keep selecting random files from the list instead of having to put together the list first. The only disadvantage is, that if files get added to the folder after you put together the initial list, then you'll have to recreate the list (or expand it) to contain the new files as well.
__________________
Reading is the foundation for all knowledge - Unknown.

Last edited by Qua; 04-17-2009 at 03:36 PM.
Reply With Quote
  #6  
Old 04-18-2009, 12:42 PM
Jazman667's Avatar
Jazman667 Jazman667 is offline
Freshman
 
Join Date: Apr 2009
Posts: 26
Default :)

Ok that makes sense i've got a tab control thats used to get to the page where the file is chosen. would it be a good idea to put this line into the click action of the tab page?
Reply With Quote
  #7  
Old 04-19-2009, 10:26 AM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
Default

It all depends on which logic you want to use. If you want to iterate and list the files everytime the tab page is clicked, then you should execute the code there
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->