?
Solved

VB Coding Suggestion - Reading files in a directory

Posted on 2006-07-07
2
Medium Priority
?
200 Views
Last Modified: 2010-07-27
I am just getting started with visual basic and was wondering if someone can show me an example or the basic commands necessary to read in files from a specified directory.  For example, the user would input the file path containing all the files to be searched.   The program would just read in each file from the specified directory.  Sounds simple.  

Input:
Search Path:  C:\Myfiles\Mytextfiles

Output:

 File Name: file1.txt
                 file2.pdf
                 file3.jpeg
                 file4.tiff
                 file5.txt

thanks
0
Comment
Question by:Hojoformo
2 Comments
 
LVL 8

Accepted Solution

by:
Shakti109 earned 2000 total points
ID: 17063334

To just "list" the files in a directory you could use something like :

Add a textbox to a windows form, then add a button.

In the button.click event of the button add the following code :

      Dim fileEntries() As String = System.IO.Directory.GetFiles(TextBox1.Text)
      Dim filename As String
      For Each filename In fileEntries
         Console.WriteLine(filename)
      Next

This will output each filename from the specified directory to the console window.

You could do something like add the filenames to a listbox on the form.

That would look like :

      Dim fileEntries() As String = System.IO.Directory.GetFiles(TextBox1.Text)
      Dim filename As String
      For Each filename In fileEntries
         listbox1.items.add(filename)
      Next


If you use the object browser inside of the IDE, you can look at the .GetFiles method (it has two variants, one accepts a pattern filter). This should point you in the right direction to start learning about File IO.
0
 
LVL 12

Expert Comment

by:Praveen Kumar
ID: 17065188
Even nice comment from Shakti, Here is a recursive function to Navigate all files in a Directory and its Sub Directories..

Private Sub ShowAllFiles(ByVal dir as String)

Try
      For Each fname as String in Directory.GetFiles(dir)
                 listbox1.items.add(filename)
      Next

      For Each subdir as String in Directory.GetDirectories(dir)
                 ShowAllFiles(subdir)
      Next
Catch
   'May be Drive not ready
End Try
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 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