My Code
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
I have a directory with nested folders. With many children and each child having 4 subchilds and each sub child has 4 files.
Parent
...Child
......subchild1
.........File 1
.........File 2
.........File 3
.........File 4
......subchild2
......subchild3
......subchild4
My Intent is to look at each child then bring merge all of the files into one txt file and then move onto the next subchild.
Pseudo Code might look like
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
What is happening is that when going through the
for each xItem in filArr
the next series of iText is occurring which I can see through the watch. This happens even if I have a break at the first next.
How can I correct this?
**Edit*****
For those that might want to test this out here is my entire code.
It requires 2 listboxes, 2 labels , 2 buttons
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
In your second For loop, change pathNum + xItem.ToString
to 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
Or
For Each f In lbFiles.Items
fileReader = File.OpenText(f)
temp = fileReader.ReadToEnd
File.AppendAllText(partPath + "\2100\" + "2100_Merged.txt", temp)
Next