Solved

VB Coding Suggestion - Reading files in a directory

Posted on 2006-07-07
2
189 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 500 total points
Comment Utility

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:Mamtha1982
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now