2015-03-31 73 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