读取没有行终止符的文件的第1行

时间:2015-03-31 14:56:52

标签: vb.net

我试图读取.txt文件的第一个非空行,并将其存储在一个文件夹中,该文件夹的名称是.txt的第一个有效行。

所以我的C:/Test/new1.txt以2个空行开头,然后是:

 example
 this is an example

我希望将其重命名/复制到C:/Test/example.txt

我有这个递归的函数,并在我想要的所有文件夹和子文件夹中获取所有.txt文件。现在我尝试读取第一个有效行,它的作用也是如此,但是当我移动/复制它时,我总是会收到错误:路径中的非法字符。

我试图修剪标题,以取代vbLf。等...无济于事

   singleFile = New IO.FileInfo(Fullpath)
   Dim sr As New StreamReader(singleFile.FullName)
   Dim title As String = Trim(sr.ReadLine).Replace(vbCrLf, "").Replace(vbLf, "").Replace(vbCr, "")
   While title = ""
         title = Trim(sr.ReadLine).Replace(vbCrLf, "").Replace(vbLf, "").Replace(vbCr, "")
   End While
   filepath = singleFile.Directory.FullName & "\" & title & ".txt"
   FileSystem.CopyFile(singleFile.FullName, filepath)

但是流读取器仍然试图在标题的末尾放置一个行终止符,我该如何摆脱它呢?

我总是在标题错误中得到非法字符,而我的文件路径= c:/ test / exemple .txt
(注意例子和.txt之间的空格)

编辑:

问题不是回车,似乎我尝试过滤的导入文件之一有另一个ascii char作为"行终止符"。所以我的问题可能会扭曲如何从字符串中删除所有空白或特殊字符

3 个答案:

答案 0 :(得分:1)

试试这个:

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fullPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "new1.txt")
        Dim fileText As String = String.Empty
        If System.IO.File.Exists(fullPath) Then
            fileText = System.IO.File.ReadAllText(fullPath)
        Else
            Throw New System.IO.FileNotFoundException("The specified file was not found.")
        End If
        Dim newName As String = FirstNonBlankLine(fileText) & ".txt"
        Dim originalPath As String = System.IO.Path.GetDirectoryName(fullPath)
        Dim newPath As String = System.IO.Path.Combine(originalPath, newName)
        MsgBox(newPath)
        System.IO.File.WriteAllText(newPath, fileText)
    End Sub
    Function FirstNonBlankLine(fileText As String) As String
        fileText = fileText.Replace(vbLf, vbCr).Replace(vbCr & vbCr, vbCr)
        Dim lines As String() = fileText.Split({vbCr}, StringSplitOptions.RemoveEmptyEntries)
        Return lines(0)
    End Function
End Class

答案 1 :(得分:1)

Dim Path As String = "C:/Test/new1.txt"
Dim Str As String = IO.File.ReadAllText(Path).Trim
IO.File.Copy(Path, "C:/Test/" + Split(Str, vbCrLf)(0) + ".txt") 'With Lines
'Or write the file without Lines
'IO.File.WriteAllText("C:/Test/" + Split(Str, vbCrLf)(0) + ".txt", Str)

编辑:     好的,您可以检查ASCII值

 Dim Path As String = "C:/Test/new1.txt"
    Dim Str As String = IO.File.ReadAllText(Path).Trim
    IO.File.Copy(Path, "C:/Test/" + GetChars(Split(Str, vbCrLf)(0)) + ".txt")



    Function GetChars(Str As String) As String
    Dim Final As String = ""
    Dim NoInPath As String = "/\*:<>?|" + ChrW(34) ' (34=")
    For Each x As Char In Str
        Try
            If Asc(x) >= 32 And Asc(x) <= 126 Then 'English Language
                If NoInPath.Contains(x) = False Then Final += x
            End If
        Catch ex As Exception
        End Try
    Next
    Return Final
End Function

答案 2 :(得分:0)

我想大多数类似情况的答案都是使用正则表达式来简单地忽略所有不在你想要的指定范围内的字符。