加入文本文件然后保存,然后使用相同的名称和内容拆分它们

时间:2018-03-13 16:49:56

标签: vb.net join merge split

我有这个代码,它将文件夹中每个文本文件的名称和内容组合成一个字符串

 Dim dir As New DirectoryInfo("D:\txtfiles\")
    Dim fiArr As FileInfo() = dir.GetFiles()
    Dim fri As FileInfo

    For Each fri In fiArr
        Dim gg As Integer = 300000
        If gg > fri.Length Then

            File.AppendAllText("1.txt", "|*|" & fri.Name & "|*|", Encoding.UTF8)
            File.AppendAllText("1.txt", File.ReadAllText(fri.FullName), Encoding.UTF8) 'The text file will be created if it does not already exist  

        End If
    Next fri

现在我需要再次拆分它们,用它的名称和内容保存每个文本文件 拆分器必须是“| * |”

1 个答案:

答案 0 :(得分:2)

Private Sub CreateFiles(dest As String, s As String)
    Dim a = s.Split("|*|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
    For i As Integer = 0 To a.Count() - 2 Step 2
        File.WriteAllText(dest & a(i), a(i + 1))
    Next
End Sub

dest是您希望将文件保存到的目录 s是您的代码创建的文件的内容

请注意!别忘了在字符串dest

中添加反斜杠