|
-
Aug 19th, 2008, 07:48 AM
#1
Thread Starter
New Member
Opening Multiple files
I don't ask for help much until I've literally pissed myself off trying it but i am making a simple form for a massage program and on this form you will be able to load multiple lists, remove 1 string of duplicate entries if it exists, sort the file alphabetically and save the file and be able to repeat this process as many times as needed.
as of right now the load function will only load 1 file at a time using the following code:
Code:
Dim AllText As String = "", LineOfText As String = ""
OpenFileDialog1.Filter = "Text files (*.TXT)|*.TXT"
OpenFileDialog1.ShowDialog() 'display Open dialog box
If OpenFileDialog1.FileName <> "" Then
Try 'open file and trap any errors using handler
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1) 'read lines from file
LineOfText = LineInput(1)
'add each line to the AllText variable
AllText = AllText & LineOfText & vbCrLf
Loop
Label1.Text = OpenFileDialog1.FileName 'update label
TextBox1.Text = AllText 'display file
Catch
MsgBox("Error opening file.")
Finally
FileClose(1) 'close file
End Try
End If
End Sub
The code for the save file will save the file but it changes on every instance to some odd autosave function. For example Open file lol save as lol2 [works fine until this part] then open file rotfl (this is when the file lol2 will be overwritten with the file rotfl)
Code:
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
PrintLine(1, TextBox1.Text) 'copy text to disk
FileClose(1)
End If
End Sub
any suggestions? i thought this out and made this pretty clear
 Originally Posted by HBDev
Being that this is illegal in 71 U.S. states including New Failico, Califailia, and Failida you are sentenced to death by lethal injection of fail.

-
Aug 19th, 2008, 07:51 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 19th, 2008, 07:52 AM
#3
Re: Opening Multiple files
theres an openfiledialog property that allows multiselect
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 19th, 2008, 08:17 AM
#4
Thread Starter
New Member
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
 Originally Posted by HBDev
Being that this is illegal in 71 U.S. states including New Failico, Califailia, and Failida you are sentenced to death by lethal injection of fail.

-
Aug 19th, 2008, 09:07 AM
#5
Re: Opening Multiple files
 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
-
Aug 19th, 2008, 09:41 AM
#6
Thread Starter
New Member
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
 Originally Posted by HBDev
Being that this is illegal in 71 U.S. states including New Failico, Califailia, and Failida you are sentenced to death by lethal injection of fail.

-
Aug 19th, 2008, 10:31 AM
#7
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.
My usual boring signature: Nothing
 
-
Aug 19th, 2008, 06:24 PM
#8
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.
-
Oct 13th, 2008, 03:44 PM
#9
Lively Member
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!!
-
Oct 13th, 2008, 03:57 PM
#10
Re: Opening Multiple files
 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.
-
Oct 14th, 2008, 08:47 AM
#11
Lively Member
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.
Last edited by dbansal; Oct 14th, 2008 at 08:54 AM.
-
Oct 14th, 2008, 02:30 PM
#12
Lively Member
Re: Opening Multiple files
any help would be highly appreciated!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|