none
copying multiple files RRS feed

  • Question

  • Hello all,

    It only copies the first file. What I am doing wrong?

            Dim the_folder As String = qc_atts & qc_year & "\" & qc_ncr
            Dim the_name As String = DateTime.Now.ToString("yyyy_MMdd_Hmmss.fff")
    
            Dim myStream As System.IO.Stream = Nothing
            Dim openFileDialog1 As New OpenFileDialog()
    
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            openFileDialog1.FileName = "*.*"
            openFileDialog1.Filter = "All (*.*, *.*) | *.*; *.*"
            openFileDialog1.Multiselect = True
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try
                    myStream = openFileDialog1.OpenFile()
                    If (myStream IsNot Nothing) Then
                        Do While myStream IsNot Nothing
                            System.IO.File.Copy(openFileDialog1.FileName.ToString.Trim, the_folder & "\" & the_name & "_" & System.IO.Path.GetFileName(openFileDialog1.FileName))
                        Loop
                    End If
                Catch Ex As Exception
                    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message, app_cia)
                Finally
                    If (myStream IsNot Nothing) Then
                        myStream.Close()
                    End If
                End Try
            End If

    Please Help

    John

    Friday, September 6, 2019 5:49 PM

Answers

  • Hi

    Seems to me that all you want to do is copy selected renamed files. Why you are trying to use a FileStream?

    You do not cycle through the selected files - that is why you only get one file copied.

    Check this code to see if it does what you want.

    Dim the_folder As String = qc_atts & qc_year & "\" & qc_ncr
    Dim openfile As New OpenFileDialog
    With openfile
    	.Multiselect = True
    	.FileName = "*.*"
    	.Filter = "All (*.*, *.*) | *.*; *.*"
    End With
    If openfile.ShowDialog = DialogResult.OK Then
    	For Each f As String In openfile.FileNames
    		Dim the_name As String = DateTime.Now.ToString("yyyy_MMdd_Hmmss.fff")
    		Dim fn As String = the_folder & "\" & the_name & "_" & IO.Path.GetFileName(f)
    		Try
    			IO.File.Copy(f, fn)
    		Catch Ex As Exception
    			MessageBox.Show(Ex.Message & vbCrLf & "Failed to Copy: " & f)
    		End Try
    	Next
    End If


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 6, 2019 6:42 PM
    • Marked as answer by Bre-x Friday, September 6, 2019 6:52 PM
    Friday, September 6, 2019 6:41 PM

All replies

  • Hi

    Seems to me that all you want to do is copy selected renamed files. Why you are trying to use a FileStream?

    You do not cycle through the selected files - that is why you only get one file copied.

    Check this code to see if it does what you want.

    Dim the_folder As String = qc_atts & qc_year & "\" & qc_ncr
    Dim openfile As New OpenFileDialog
    With openfile
    	.Multiselect = True
    	.FileName = "*.*"
    	.Filter = "All (*.*, *.*) | *.*; *.*"
    End With
    If openfile.ShowDialog = DialogResult.OK Then
    	For Each f As String In openfile.FileNames
    		Dim the_name As String = DateTime.Now.ToString("yyyy_MMdd_Hmmss.fff")
    		Dim fn As String = the_folder & "\" & the_name & "_" & IO.Path.GetFileName(f)
    		Try
    			IO.File.Copy(f, fn)
    		Catch Ex As Exception
    			MessageBox.Show(Ex.Message & vbCrLf & "Failed to Copy: " & f)
    		End Try
    	Next
    End If


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 6, 2019 6:42 PM
    • Marked as answer by Bre-x Friday, September 6, 2019 6:52 PM
    Friday, September 6, 2019 6:41 PM
  • Thank you!!!!

    I just added : .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

    :)

    John

    Friday, September 6, 2019 6:52 PM