Re: Opening Multiple files
thats all legacy vb6 code. you should use io.file.readalltext or io.file.readalllines and io.file.writealltext or io.file.writealllines
Re: Opening Multiple files
theres an openfiledialog property that allows multiselect
Re: Opening Multiple files
i tried the multi file select adn it didn't work correctly for me. It selected the files but only opened the 1st file selected
Re: Opening Multiple files
Quote:
Originally Posted by r33k
i tried the multi file select adn it didn't work correctly for me. It selected the files but only opened the 1st file selected
It's not that IT only opened the first one. It's that YOU only opened the first one. The FileNames property is just a String array that contains the paths of the selected files. It has no magic. It doesn't open anything. If you want to do anything with those files then it's up to YOU to loop through them and use each path to open the file in whatever way is appropriate. You'd most likely use a For Each loop. As a simple example:
VB.NET Code:
Using ofd As New OpenFileDialog With {.Multiselect = True}
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
For Each filePath As String In ofd.FileNames
MessageBox.Show(IO.File.ReadAllText(filePath), filePath)
Next
End If
End Using
Re: Opening Multiple files
that opens the files in a message box what i was designing this program for was to be a text handler that would load a list or multiple lists, sort them, alphabetically and remove 1 or the duplicates in the list if they existed then save it into a text file.
Your method doesn't help and nothing I have tried have given me the results that I need
Re: Opening Multiple files
Actually, his method answered most of your question. He was using a messagebox to show you that you could open multiple files, not showing you want you needed to do with those multiple files.
I wasn't clear myself as to what you wanted to do with the files when opened, as you have multiple options from what you have stated. Mostly, it looks like you want to end up with the files combined. If that is what you want, then you will do a loop like JM posted, but where he has a Messagebox to display the lines, you will use ReadAllLines to fill an array from each file in turn, then add the lines from the array to your growing list. Should be able to do that in one step with List.AddRange.
You can then sort, remove, etc.
However, the problem will arise when you save the data. It isn't clear whether you want to save the whole list (that is composed of one or many files) back to a single file, or back to the original files. If it is the first, then you would do something like WriteAllLines (don't even know if it exists, offhand, as I haven't looked, but at worst you loop through the list writing each individual line, which I have done). If it is the second choice, that of writing multiple files, you will have a few more decisions to make. I won't go into that here, in hopes that you meant the first option.
In any case, you will start with JM's stubb, and simply replace the MessageBox with proper handling of the array returned by ReadAllLines.
Re: Opening Multiple files
My code does exactly what it was supposed to do. It provides an EXAMPLE of how you can access each and every file selected by the user in an OpenFileDialog. What you actually do with those files is up to you. If someone says they are providing an example, assume that it is NOT a full solution and that you need to use your head a bit to make use of the principles demonstrated.
Re: Opening Multiple files
jmcilihinney,
I was wondering if you could help with my code:
Code:
Private Sub MultivalidateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultivalidateToolStripMenuItem.Click
With OpenFileDialog2
.InitialDirectory = "C:\"
.Filter = "All Files|*.*|Drawbase CMD File (.cmd) |*.cmd|Text Files (.txt) |*.txt"
.FilterIndex = 2
.Multiselect = True
End With
Using OpenFileDialog2
If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
For Each filename As String In OpenFileDialog2.FileNames
MessageBox.Show(OpenFileDialog2.FileName)
Next
End If
End Using
It seems to loop because it pops up 3 message boxes. however, even though i selected 3 different files, the message box pops up with the same file name each time.
Thanks for your helP!!
Re: Opening Multiple files
Quote:
Originally Posted by dbansal
jmcilihinney,
I was wondering if you could help with my code:
Code:
Private Sub MultivalidateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultivalidateToolStripMenuItem.Click
With OpenFileDialog2
.InitialDirectory = "C:\"
.Filter = "All Files|*.*|Drawbase CMD File (.cmd) |*.cmd|Text Files (.txt) |*.txt"
.FilterIndex = 2
.Multiselect = True
End With
Using OpenFileDialog2
If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
For Each filename As String In OpenFileDialog2.FileNames
MessageBox.Show(OpenFileDialog2.FileName)
Next
End If
End Using
It seems to loop because it pops up 3 message boxes. however, even though i selected 3 different files, the message box pops up with the same file name each time.
Thanks for your helP!!
What you're doing there is like saying "for each door on a car open the driver's door". If there are are 5 doors on the car you're going to open the driver's door 5 times, yes? What you need to do is "for each door on a car open that door", then you'll open all 5 doors once each. In your case you're saying "For Each filename As String In OpenFileDialog2.FileNames" so that's what you should be showing in the MessageBox.
Re: Opening Multiple files
All,
Apologies for the multiple postings about the above question. I admit I was being impatient.
One last question to wrap my question up. I have gotten the multiple files to open and the loop indeed works. I know because the message box pops up and everytime I click ok my text boxes are filled with the appropriate information. However, my save function is not working correctly. It only saves the first file and then edits that first file with the information from the next file. In other words, it creates one file and writes to it x times.
Code:
Private Sub MultivalidateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultivalidateToolStripMenuItem.Click
With OpenFileDialog2
.InitialDirectory = "C:\"
.Filter = "All Files|*.*|Drawbase CMD File (.cmd) |*.cmd|Text Files (.txt) |*.txt"
.FilterIndex = 2
.Multiselect = True
End With
With SaveFileDialog2
.InitialDirectory = "C:\test\"
.Filter = "All Files|*.*|Drawbase CMD File (.cmd) |*.cmd|Text Files (.txt) |*.txt"
.FilterIndex = 2
End With
Using OpenFileDialog2
If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
For Each filename As String In OpenFileDialog2.FileNames
Dim filestream3 As System.IO.Stream = OpenFileDialog2.OpenFile
Using reader3 As New System.IO.StreamReader(filename.ToString)
tbinv.Text = reader3.ReadToEnd
End Using
MessageBox.Show(filename)
filestream3.Close()
Dim lines As String() = tbinv.Lines
Dim searchStrings() As String = {"CMD Tbar", "CMD Menu"}
Dim builder As New System.Text.StringBuilder
For i As Integer = 0 To lines.GetUpperBound(0)
For j As Integer = 0 To searchStrings.GetUpperBound(0)
If lines(i).IndexOf(searchStrings(j)) > 0 Then
builder.AppendLine(String.Format("The String '{0}' was found on line {1}", searchStrings(j), i + 1))
End If
Next
Next
tbinvout.Text = (builder.ToString())
Using SaveFileDialog2
Dim filename1 As String = IO.Path.GetFileNameWithoutExtension(OpenFileDialog2.FileName) & ".txt"
SaveFileDialog2.FileName = filename1
Dim fileStream2 As System.IO.Stream = SaveFileDialog2.OpenFile
Using reader2 As New System.IO.StreamWriter(fileStream2)
reader2.Write(tbinvout.Text)
End Using
fileStream2.Close()
End Using
Next
End If
End Using
End Sub
Thanks! Again, apologies for the multiple posts.
Re: Opening Multiple files
any help would be highly appreciated!!!