Copying multiple files

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
Is there a way to copy all files of a certain type from one folder to another without using loads of code with a loop?

example:

Filecopy("C:\anyfolder\*.jpg","destinationfolder")

Thnx
 
Well, it wouldnt be loads of code, but it would be a loop. Youre only looking at about a half-dozen lines. Assuming you have a FolderBrowserDialog called fbd:
Code:
        If fbd.ShowDialog = DialogResult.OK Then

            Dim dirInfo As New DirectoryInfo(fbd.SelectedPath)

            Dim files() As FileInfo = dirInfo.GetFiles("*.jpg")
            Dim fInfo As FileInfo
            Dim newPath As String

            set newpath before this
            For Each fInfo In files
                File.Copy(fInfo.Name, newPath & "\\" & fInfo.Name)
            Next fInfo

        End If
Air-coded so there might be a bug in there, but you should get the idea. :)
 
you could always use the SHFileOperation API mate [Broken External Image]:http://www.computerhelp.forum/images/smilies/wink.gif you can specify wildcards ( eg: *.jpg , *.bmp , *.whatever ) to allow only certain files to be copied / moved / deleted.

heres a quick example...
Code:
	Public Structure SHFILEOPSTRUCT
		Public hWnd As Integer
		Public wFunc As Integer
		Public pFrom As String
		Public pTo As String
		Public fFlags As Integer
		Public fAborted As Integer
		Public hNameMaps As Integer
		Public sProgress As String
	End Structure
 
	Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
	Private Const FO_COPY As Int32 = &H2
	Private Const FO_DELETE As Int32 = &H3
	Private Const FO_MOVE As Int32 = &H1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
		Dim shStructure As New SHFILEOPSTRUCT
		///here i specify the wildcard *.jpg ...
		Dim currentpath As String = "C:\Documents and Settings\den\My Documents\My Pictures\imgs\*.jpg" /// images in folder to copy
		/// new path to COPY the images to ( jpg only in this case )
		Dim newpath As String = "E:\imgs\" /// new path
		With shStructure
			.wFunc = FO_COPY
			.pFrom = currentpath
			.pTo = newpath
		End With
		SHFileOperation(shStructure)
 
	End Sub
hope it helps :)
 
Back
Top