我的代码
Dim fileReader As StreamReader
Dim fileName, temp, strVal As String
For i As Integer = 0 To lbFolder.Items.Count - 1
Dim iText As String = CStr(lbFolder.Items(i))
Dim partPath As String = lblPath.Text + "\" + iText
Dim pathNum As String = partPath + "\2100\"
Dim directory As New DirectoryInfo(pathNum)
Dim fileArr As FileInfo() = directory.GetFiles() ' Get a reference to each file in that directory.
' Display the names of the files.
Dim xItem As FileInfo
For Each xItem In fileArr
lblFname.Text = xItem.ToString
strVal = pathNum & xItem.ToString
lbFiles.Items.Add(strVal)
Next
For j = 0 To lbFiles.Items.Count - 1
fileReader = File.OpenText(pathNum + xItem.ToString)
temp = fileReader.ReadToEnd
File.AppendAllText(partPath + "\2100\" + "2100_Merged.txt", temp)
Next j
lbFiles.Items.Clear()
Next i
End Sub
我有一个嵌套文件夹的目录。有许多孩子,每个孩子有4个子孙,每个子孩子有4个文件。
父
...儿童
...... subchild1
.........文件1
.........文件2
.........文件3
.........文件4
...... subchild2
...... subchild3
...... subchild4
我的意图是查看每个孩子,然后将所有文件合并到一个txt文件中,然后转到下一个子项。
伪代码可能看起来像
open Subchild folder
for each file in subchild folder
listbox.additem(file)
next
for each item in listbox
add to temp file
next
output tmp file
next subchild
正在经历的是
for each xItem in filArr
下一系列的iText正在发生,我可以通过手表看到。 即使我在第一次休息时休息,也会发生这种情况。
我该如何纠正?
** *****编辑
对于那些可能想要测试它的人来说,这是我的整个代码。
它需要2个列表框,2个标签,2个按钮
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'open folders
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = Application.StartupPath
If DialogResult.OK = dialog.ShowDialog() Then
lblPath.Text = dialog.SelectedPath
End If
For Each folder As String In System.IO.Directory.GetDirectories(lblPath.Text)
lbFolder.Items.Add(Path.GetFileName(folder))
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim found700 As Boolean
Dim fileReader As StreamReader
Dim fileName, temp, strVal As String
For i As Integer = 0 To lbFolder.Items.Count - 1
Dim iText As String = CStr(lbFolder.Items(i))
Dim partPath As String = lblPath.Text + "\" + iText
Dim pathNum As String = partPath + "\2100\"
Dim directory As New DirectoryInfo(pathNum)
Dim fileArr As FileInfo() = directory.GetFiles() ' Get a reference to each file in that directory.
' Display the names of the files.
Dim xItem As FileInfo
For Each xItem In fileArr
lblFname.Text = xItem.ToString
strVal = pathNum & xItem.ToString
lbFiles.Items.Add(strVal)
Next
For j = 0 To lbFiles.Items.Count - 1
fileReader = File.OpenText(pathNum + xItem.ToString)
temp = fileReader.ReadToEnd
File.AppendAllText(pathNum + "2100_Merged.txt", temp)
Next j
lbFiles.Items.Clear()
Next i
End Sub
答案 0 :(得分:1)
在第二个For循环中,将pathNum + xItem.ToString
更改为lbFiles.Items.Item(j)
For j = 0 To lbFiles.Items.Count - 1
fileReader = File.OpenText(lbFiles.Items.Item(j))
temp = fileReader.ReadToEnd
File.AppendAllText(partPath + "\2100\" + "2100_Merged.txt", temp)
Next j
或者
For Each f In lbFiles.Items
fileReader = File.OpenText(f)
temp = fileReader.ReadToEnd
File.AppendAllText(partPath + "\2100\" + "2100_Merged.txt", temp)
Next