2017-05-10 55 views
0

我希望能夠從存儲在文本文件中的列表中創建新文件夾。 的名稱存儲像來自列表中的新文件夾,來自vb.net中的文本文件

test1 
test2 
test3 

所以到目前爲止我的代碼,將存儲在另一個文本文件來創建新的文件夾的路徑,(這是在給定的父文件夾的最古老的文件夾)「Foldercreation.txt」

然後用我想創建的文件夾的名稱「Folderstocreate.txt」打開文件,並將它們全部存儲在filereader2中。

但是當試圖爲每一行創建文件夾時什麼也沒有發生。

我現在的代碼;

Dim fileReader, filereader2 As System.IO.StreamReader 
    Dim stringreader, parfolder As String 
    Dim path, foldername As List(Of String) 
    Dim count As Byte 

    If MsgBox("Are you sure you want to create these folders?, 

Before clicking yes, make sure EVERYONE is out of paperport & you have entered the correct numbers.", MsgBoxStyle.YesNo, "WARNING!") = MsgBoxResult.Yes Then 

      If strnumbx.Text = "" Then 
       MsgBox("You have not entered a start number for the folders.", MsgBoxStyle.OkOnly, "Error") 
      End If 

      'Loads a text file at the given location, to read to. 
      fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\Data\Test\Foldercreation.txt") 
      'Set stringreader as the read line from the file 
      stringreader = fileReader.ReadLine() 

      path = System.IO.Directory.GetDirectories(stringreader).ToList 
      path.Sort() 
      count = path.Count - 1 
      parfolder = path(count) 

      'System.IO.Directory.CreateDirectory(parfolder & "\test") 



      filereader2 = New StreamReader("C:\Data\Test\Folderstocreate.txt", True) 

      filereader2.ReadToEnd() 

      For Each line In filereader2.ReadToEnd() 
       System.IO.Directory.CreateDirectory(parfolder & fileReader.ReadToEnd(count - 1)) 
       count = count + 1 
      Next 
     End If 

     fileReader.Close() 
     filereader2.Close() 
+0

您正在使用'ReadAllLine',但您說您的值由空格分隔。這是什麼? – Mederic

+0

這個網站改變了格式,他們在不同的線路 –

回答

1

這個函數會這樣做,但您可能想要進行一些異常處理。

Directory.CreateDirectory將創建所有父文件夾,如果它們不存在。

Private Sub CreateAllDirectories(ByVal strFileList As String) 
    Dim strDirectories As String() = File.ReadAllLines(strFileList) 

    For Each strDirectory As String In strDirectories 
     If Not Directory.Exists(strDirectory) Then 
      Directory.CreateDirectory(strDirectory) 
     End If 
    Next 
End Sub 
+0

我建議編輯,因爲你寫的功能有點快,也不費心編寫它,哈哈,但我包括一個關於進口的筆記 – Mederic

+0

雅我想過注意但是太懶惰了XD – NMGod

+0

檢查'If Not String.IsNullOrWhiteSpace(strDirectory)那麼......'會很謹慎,以防在那裏有空行,以及你提到的異常處理。 –