Solved

VB Coding Suggestion - Reading files in a directory

Posted on 2006-07-07
2
191 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
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:Mamtha1982
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL syntax in VB.net 5 41
VB.Net - Project Software to help the Developer 7 36
Sql server insert 13 32
Sending receiving text messages in vb.net 15 38
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

832 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