2015-03-31 57 views
0

我试图读取.txt文件的第一个非null行,并将其存储在文件夹中,该文件夹的名称将为该.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) 

但仍StreamReader的试图把一个行终止在标题的结尾,我怎么能摆脱它?

我总是在标题错误非法字符,和我的文件路径= C:/测试/为例.TXT
(注意为例和.txt之间的空间)

编辑:

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

回答

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 
+0

nope,测试\t .txt就是我所得到的(除了VBLF之外,在.txt中可能还有其他垃圾......我认为这是一个考虑大小的选项卡,我没有在测试中使用过,所以我会尝试混合添加一个修剪顶部的测试)...文件没有创建自己,他们被导入) – 2015-03-31 18:24:17

+0

感谢您的新语法,但修剪摆脱了选项卡,但不是在结束的空白空间标题。 :/ ... – 2015-03-31 18:28:02

+0

您可以上传文本文件的未修改副本吗? – 2015-03-31 19:34:14

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 
+0

感谢您的信息,没有帮助我的特殊问题,但至少我现在可以继续挖掘一点。 – 2015-03-31 18:34:56