Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,065 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,775 people online right now. Registration is fast and FREE... Join Now!




Copying multiple files from open file dialog

 
Reply to this topicStart new topic

Copying multiple files from open file dialog

dandes
1 Feb, 2008 - 09:37 AM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 4

I am trying to copy multiple files from an Open Files Dialog. The path of the files selected to copy should be displayed in a richtext doc in the form. At on point, the files selected displayed...but now they do not. I also managed to get one file to copy...but in revising the code...canot get back to that point where everything almost worked. Any suggestions would be appreciated.


CODE

    Dim strFileName As String = ""
    Dim path As String = "C:\"


  Private Sub btnSendFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFiles.Click
        Dim TotalFile As String = ""
        OpenFD.Multiselect = True
        Dim CopyLoc = path & strFileName

        If OpenFD.ShowDialog = DialogResult.OK Then
            For Each s As String In OpenFD.FileName
                TotalFile = TotalFile & s & Environment.NewLine
                rtxFiles.Text = TotalFile
                File.Copy(OpenFD.FileName, CopyLoc & "\" & OpenFD.FileName.Substring(OpenFD.FileName.LastIndexOf("\")))
            Next

        End If
    End Sub

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 1 Feb, 2008 - 10:44 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Copying Multiple Files From Open File Dialog
1 Feb, 2008 - 11:09 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
If by copying you mean copy the file itself from one folder to C:\ then you can use something like this... (a modification of your function)

CODE

Dim strFileName As String = ""
Dim path As String = "C:\"


Private Sub btnSendFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFiles.Click

        Dim TotalFile As String = ""
        OpenFD.Multiselect = True

        If OpenFD.ShowDialog = DialogResult.OK Then
            ' For each filename in the list of selected FileName"S"
            For Each s As String In OpenFD.FileNames
                ' Append the filename on a line and add the newline char
                rtxFiles.Text &= s & Environment.NewLine

                ' Use the filename as the source, then build the path to C:\ using the filename of the current file.
                ' Result, all files selected in the openfile dialog will be copied to C:\ with the same names.
                ' eg. D:\miscfolder\helloworld.vb will be copied to C:\ as C:\helloworld.vb
                File.Copy(s, path & System.IO.Path.GetFileName(s))
            Next

        End If
End Sub


The in code comments will tell you what is going on at each step and hopefully this is what you were looking to do with this code. If not, can you be more specific as to your intentions? Thanks!

"At DIC we be file copying code ninjas!" decap.gif

User is online!Profile CardPM
+Quote Post

dandes
RE: Copying Multiple Files From Open File Dialog
1 Feb, 2008 - 02:31 PM
Post #3

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 4

That works great! Thanks for the help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month