Solved

vb.net - read files in subdirs

Posted on 2011-09-21
10
265 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
Comment Utility
simply use the Directory.GetFiles() method to retrieve ALL the files in a directory
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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
Comment Utility
remove the .Distinct
0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now