1 Replies - 14621 Views - Last Post: 04 April 2008 - 07:59 PM Rate Topic: -----

#1 keithmoloney   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 04-April 08

COPY Multiple Files at the same time

Posted 04 April 2008 - 07:14 AM

*I AM USING VB.NET 2005 EXPRESS*
I want to copy multiple files from one directory to another using the multiselect function of the OpenFileDialog.
Even when I select multiple files, it still only copies one so maybe i need some sort of loop?
Please advise.
Thanks.

'i have code that will read multiple files from the OpenFileDialog and display their names in a listbox.
'What i want to do is not display these files in a listbox but copy them all at once to a directory located elsewhere.
'my code for the listbox is:
Public Class TEST

	Private Sub btnFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiles.Click
		Dim sFile() As String
		Dim iCount As Integer

		With ofd1
			.InitialDirectory = "C:\windows"
			.FileName = vbNullString						'Clear out previous filename
			.Filter = "Log Files (*.log)|*.log|All Files (*.*)|*.*" 'Set the input mask
			.FilterIndex = 1								'Set index of filter
			.Multiselect = True							 'Allow selecting of multiple files
			.CheckFileExists = True						 'Only select existing files
			.ShowReadOnly = True							'Hide read only files
			.Title = "Import Files"						 'Set caption of OFD
			If .ShowDialog() = Windows.Forms.DialogResult.OK Then		 'Only do if OK was clicked
				For iCount = 0 To .FileNames.GetUpperBound(0)  'Loop through the file names

					lstFiles.Items.Add(.FileNames(iCount))  'Add files to list box

				Next
			End If
		End With

	End Sub
End Class

'...............................................................................
'my code for the form where i want to do the copying is:
'...............................................................................
'it will only let me copy one file at a time.

Imports System.IO
Public Class FileCopy



	Private Sub FileCopy_Load(ByVal sender As System.Object, ByVal e _
	As System.EventArgs) Handles MyBase.Load

	End Sub



	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _
	System.EventArgs) Handles Button2.Click

		'Dim i As Integer
		Dim strDest As String

		strDest = "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest"
		Try
			'The manager and librarian will be emailed. In the email, they will be informed if the release was successful or not.
			If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then

				'Here we will enter the directory of the release folder.

				'TODO - maybe put a loop here so that it repeats until all are copied?
				'For i = 0 To 50
				File.Copy(OpenFileDialog1.FileName, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\")), True)
				File.Copy(OpenFileDialog1.FileName, "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASED1" & "\" & OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\")), True)
				'i = i + 1
				'Next i
				MsgBox("File Copied Successfully to " & strDest)   'inform user that files are copied successfully													  'true allows overwrite of original
				EmailY.Main()					   'email the manager and librarian telling them the same.
			Else
				MsgBox("File Not Copied")   'inform user that files have not been copied successfully
				EmailN.Main()			   'email the manager and librarian telling them the same.
			End If
		Catch
		End Try

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Home.Show()
		Me.Hide()
	End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: COPY Multiple Files at the same time

#2 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: COPY Multiple Files at the same time

Posted 04 April 2008 - 07:59 PM

Yeah you are going to need a loop. A For Each loop will do nicely. Just iterate through the items in the FileNames (note the "s") property.

Something like this:
For Each file As String In OpenFileDialog1.FileNames
	  File.Copy(file , "C:\Documents and Settings\kmoloney\Desktop\kmoloney\RELEASEDtest" & "\" & file.Substring(file.LastIndexOf("\")), True)
Next

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1