Link to home
Start Free TrialLog in
Avatar of linuxrox
linuxroxFlag for United States of America

asked on

VB.NET copy entire folder and subfolders to destination and ignore thumbs.db

Hello.  Currently here is the code I use to copy an entire folder:

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("\\share\folder", "\\destshare\folder", True)

Open in new window


The issue I'm having is a file like "thumbs.db" when trying to be copied results in an error:

"Error is could not complete operation on some files and directories.  see the data property of the exception for more details."

is there any method to copy an entire folder and it's contents and ignore certain file types?  Much appreciated if someone can show me a solution for such a thing.

thank you very much.
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try following method

Public Sub CopyFolder(sourceFolder As String, destFolder As String)
	If Not Directory.Exists(destFolder) Then
	   Directory.CreateDirectory(destFolder)
	End If
	Dim files As String() = Directory.GetFiles(sourceFolder)
	For Each file__1 As String In files
		Dim name As String = Path.GetFileName(file__1)
                If name <> "thumbs.db" Then
   		   Dim dest As String = Path.Combine(destFolder, name)
 		   File.Copy(file__1, dest)
                End If
	Next
	Dim folders As String() = Directory.GetDirectories(sourceFolder)
	For Each folder As String In folders
		Dim name As String = Path.GetFileName(folder)
		Dim dest As String = Path.Combine(destFolder, name)
		CopyFolder(folder, dest)
	Next
End Sub

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of linuxrox

ASKER

thanks!!