Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

My website
Posted

Well, it wouldn't be loads of code, but it would be a loop. You're only looking at about a half-dozen lines. Assuming you have a FolderBrowserDialog called fbd:

       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. :)

Here's what I'm up to.
  • Leaders
Posted

you could always use the SHFileOperation API mate http://www.xtremedotnettalk.com/images/smilies/wink.gif you can specify wildcards ( eg: *.jpg , *.bmp , *.whatever ) to allow only certain files to be copied / moved / deleted.

 

here's a quick example...

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...