?
Solved

vb.net - read files in subdirs

Posted on 2011-09-21
10
Medium Priority
?
274 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 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