Solved

vb.net - read files in subdirs

Posted on 2011-09-21
10
268 Views
Last Modified: 2012-05-12
hello there,
I have this great code that converts a unix text file into windows dos format.
I would like to make a new function "GetFiles" that can read all the files in the dir and subdirs
then convert each file like this

Call TextUnixToWin("c:\unix_files\file1.txt")
Option Explicit On
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        call GetFiles("c:\unix_files\")
    End Sub

    Private Sub TextUnixToWin(ByVal strFilePath As String)
        Try
            Dim outFile As New List(Of String)
            Using inFile As New IO.StreamReader(strFilePath)
                Try
                    Dim buffer As String = String.Empty
                    Try
                        While Not inFile.EndOfStream
                            buffer = inFile.ReadLine()
                            outFile.Add(buffer)
                        End While
                    Catch ex As Exception
                        Debug.Print("Error performing conversion: {0}", ex.Message)
                        Debug.Print("Press any key to exit...{0}", vbCrLf)
                    End Try
                Catch ex As Exception
                    Debug.Print("Could not open out file: {0}", ex.Message)
                    Debug.Print("Press any key to exit...{0}", vbCrLf)
                End Try
            End Using
            If outFile.Count > 0 Then
                IO.File.WriteAllLines(strFilePath, outFile)
                Debug.Print("done!")
            End If
        Catch ex As Exception
            Debug.Print("Could not open in file: {0}", ex.Message)
            Debug.Print("Press any key to exit...{0}", vbCrLf)
        End Try
    End Sub
End Class

Open in new window

0
Comment
Question by:XK8ER
  • 7
  • 3
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36576668
simply use the Directory.GetFiles() method to retrieve ALL the files in a directory
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36576688
Here is a suggestion
        Dim xFiles = Directory.GetFiles("YourPathToDirectory")
        For Each x In xFiles
            TextUnixToWin(x)
        Next

Open in new window

0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36576739
You can actually add more tests to the returned files before calling your function, e.g
Dim xFiles = Directory.GetFiles("c:\unix_files")
For Each x In xFiles
    If x.EndsWith("txt") Then TextUnixToWin(x)
Next
Private Sub TextUnixToWin(ByVal strFilePath As String)
 ' your other code here.....

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:XK8ER
ID: 36576957
nepaluz, I have a problem and its that it reads only files located in c:\unix_files
it wont read files in c:\unix_files\admin\ or c:\unix_files\docs\
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577130
Hmmmm! try this to include ALL sub directories
Dim HomeDir = "c:\unix_files"
Dim qFiles = Directory.GetDirectories(HomeDir)
Dim xFiles = Directory.GetFiles(HomeDir)
For Each y In qFiles
    xFiles = xFiles.Union(Directory.GetFiles(Path.Combine(HomeDir, y))).Distinct
Next
For Each x In xFiles
     If x.EndsWith("txt") Then TextUnixToWin(x)
Next

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 36577159
I get this
Unable to cast object of type '<DistinctIterator>d__81`1[System.String]' to type 'System.String[]'.

on line
xFiles = xFiles.Union(Directory.GetFiles(Path.Combine(HomeDir, y))).Distinct
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577180
remove the .Distinct
0
 
LVL 1

Author Comment

by:XK8ER
ID: 36577208
same thing.

would Directory.GetDirectories(HomeDir)

also be able to get files from

c:\unix_files\docs\names\page
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577223
Also, the new files will ALL be written in the HomeDir folder and NOT their original folders / sub directories. If you prefer to havethem in their original folders, then you will have to run the loop for the sub directories separately. Your code will then change to:
Dim HomeDir = "c:\unix_files"
Dim xFiles = Directory.GetFiles(HomeDir)
For Each x In xFiles
    If x.EndsWith("txt") Then TextUnixToWin(x)
Next
Dim qDirs = Directory.GetDirectories(HomeDir)
For Each y In qDirs
    Dim subFiles = Directory.GetFiles(Path.Combine(HomeDir, y))
    For Each x In qFiles
        If x.EndsWith("txt") Then TextUnixToWin(x)
    Next
Next

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577231
As for your last comment the answer will be NO. From the latest code suggestion, you can work out that you will need to get the directories in the current directory before you can get the files in those directories. The same principle would apply for the next level of directories within the subdirectory.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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 …

713 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