?
Solved

vb.net - read files in subdirs

Posted on 2011-09-21
10
Medium Priority
?
271 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
[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
  • 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

771 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