Solved

vb.net - read files in subdirs

Posted on 2011-09-21
10
266 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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