或者组合多个文本文件(vb.net)

时间:2014-08-23 12:28:39

标签: vb.net

让我在excel表上解释一下。我目录中的txt文件很少(f.txt,d.txt,s.txt,a.txt和q.txt)。每个文件都有几行文字。我希望以特定方式组合这些文件 - 它显示在屏幕截图中。

http://i.stack.imgur.com/G2ioY.png

并输出应为:

http://i.stack.imgur.com/EtRL1.png

我已经制作了一段代码,但它不起作用 - 我不知道为什么。

Dim fileEntries As String() = Directory.GetFiles("D:\dir\", "*.txt")
    ' Process the list of .txt files found in the directory. '
    Dim i As Integer = 0
    Dim filesCount As Integer = Directory.GetFiles("D:\dir\", "*.txt").Count

    Do Until i = filesCount
     'do it for every file in folder'
        i = i + 1
        Dim reader As New System.IO.StreamReader(fileEntries(i))
        Dim files() As String = reader.ReadToEnd.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        Dim lineCount = File.ReadAllLines(fileEntries(i)).Length
        Dim w As Integer = 0

        Dim dt As DataTable
        dt.Columns.Add(i)
          'add column "0" for file 1, "1" for file 2 etc.'
        Do Until w = lineCount
            dt.Rows.Add(files(w))
            'write each line in file 1 to column 0, etc.'
            w = w + 1
        Loop

    Loop

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题:

  • 您的datatable未初始化
  • w的值超过files数组
  • 的大小

注意:我使用DataSet添加每个DataTable,但如果不需要,可以将其删除。

请尝试以下代码:

Dim fileEntries As String() = Directory.GetFiles("C:\dir\", "*.txt")
' Process the list of .txt files found in the directory. '

Dim filesCount As Integer = Directory.GetFiles("C:\dir\", "*.txt").Count()

Dim ds As New DataSet()

For i As Integer = 0 To filesCount - 1
    'do it for every file in folder'
    i = i + 1
    Dim reader As New System.IO.StreamReader(fileEntries(i))

    Dim files As String() = reader.ReadToEnd().Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    Dim lineCount = File.ReadAllLines(fileEntries(i)).Length
    Dim w As Integer = 0

    Dim dt As New DataTable()
    dt.Columns.Add(i.ToString())
    'add column "0" for file 1, "1" for file 2 etc.'
    While w <> lineCount
        If files.Length = w AndAlso w <> 0 Then
            Continue While
        End If

        dt.Rows.Add(files(w))
        'write each line in file 1 to column 0, etc.'
        w = w + 1
    End While

    ds.Tables.Add(dt)
Next

答案 1 :(得分:0)

的读/写

如果您的目标如上一张图片所示,请回写一个名为output.txt的文件,然后可以在一行代码中完成。

My.Computer.FileSystem.WriteAllText("D:\dir\output.txt", String.Join(Environment.NewLine, (From path As String In Directory.GetFiles("D:\dir", "*.txt") Where IO.Path.GetFileNameWithoutExtension(path) <> "output" Select My.Computer.FileSystem.ReadAllText(path, Encoding.UTF8))), False, Encoding.UTF8)

如果你不喜欢单行,你当然可以让它更具可读性。

My.Computer.FileSystem.WriteAllText(
    "D:\dir\output.txt",
    String.Join(
        Environment.NewLine,
        (
            From
                path As String
            In
                Directory.GetFiles("D:\dir", "*.txt")
            Where
                IO.Path.GetFileNameWithoutExtension(path) <> "output"
            Select
                My.Computer.FileSystem.ReadAllText(path, Encoding.UTF8)
        )
    ),
    False,
    Encoding.UTF8
)

迭代

如果需要迭代每一行和/或每个文件,请将结果存储在本地变量中。

Dim files As IEnumerable(Of String()) = (
    From
        path As String
    In
        Directory.GetFiles("D:\dir", "*.txt")
    Select
        My.Computer.FileSystem.ReadAllText(path, Encoding.UTF8).Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
)

For Each file As String() In files
    For Each line As String In file

    Next
Next

的数据集

如果您需要从结果中创建DataSet,请使用anonymous types。这样您就可以存储文件名及其行。

Dim files = (
    From
        path As String
    In
        Directory.GetFiles("D:\dir", "*.txt")
    Select
        New With {
            Key .Name = IO.Path.GetFileNameWithoutExtension(path),
            .Lines = My.Computer.FileSystem.ReadAllText(path, Encoding.UTF8).Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        }
)

Dim data As New DataSet()

With data
    .BeginInit()
    For Each item In files
        With data.Tables.Add(item.Name)
            .BeginInit()
            .Columns.Add("Column1", GetType(String))
            .EndInit()
            .BeginLoadData()
            For Each line As String In item.Lines
                .Rows.Add(line)
            Next
            .EndLoadData()
        End With
    Next
    .EndInit()
End With