Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

VB Coding Suggestion - Reading files in a directory

Posted on 2006-07-07
2
Medium Priority
?
198 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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