Solved

VB Coding Suggestion - Reading files in a directory

Posted on 2006-07-07
2
193 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
[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
2 Comments
 
LVL 8

Accepted Solution

by:
Shakti109 earned 500 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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