Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-15
4
Medium Priority
?
1,513 Views
Last Modified: 2013-05-16
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.
0
Comment
Question by:linuxrox
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 2000 total points
ID: 39169754
The method is to write your own method and iterate the files.

Put this in a class file called Matcher.vb
Imports System.IO
Public Class Matcher
    Public Event Matched(FilePath As String, Filename As String)
    Public Event Excluded(FilePath As String, Filename As String)

    Sub Search(ByVal sDir As String, Excludes As System.Collections.Specialized.StringCollection, Optional SearchPattern As String = "*.*")
        Dim d As String
        Dim f As String

        'loop through all files in the current sub directory
        For Each f In Directory.GetFiles(sDir, SearchPattern)
            Dim fi As FileInfo = New FileInfo(f)
            If Excludes.Contains(fi.Name) Then
                RaiseEvent Excluded(f, fi.Name)
            Else
                RaiseEvent Matched(f, fi.Name)
            End If
        Next

        Try
            'loop through all sub directories
            For Each d In Directory.GetDirectories(sDir)
                'search that subfolder
                Search(d, Excludes, SearchPattern)
            Next
        Catch excpt As System.Exception
            Debug.WriteLine(excpt.Message)
        End Try
    End Sub
End Class

Open in new window


You create excludes in a collection and can respond to events as desired:

put the following in a module
Module Module1
    Dim WithEvents MyMatcher As New Matcher()

    Sub Main()
        Dim Excludes As New System.Collections.Specialized.StringCollection
        Excludes.Add("thumbs.db")

        MyMatcher.Search("c:\users\YOURUSERNAME\Pictures", Excludes)
    End Sub

    Private Sub MyMatcher_Excluded1(FilePath As String, Filename As String) Handles MyMatcher.Excluded
DEBUG.WRITLINE(String.Format("Excluded {0}", FilePath))
    End Sub

    Private Sub MyMatcher_Matched1(FilePath As String, Filename As String) Handles MyMatcher.Matched
        Debug.WriteLine(String.Format("Matched {0}", FilePath))
    End Sub
End Module

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39170884
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

0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 2000 total points
ID: 39171328
I forgot to mention that in the events you can copy, delete or whatever. I wrote my class as a skeleton to a matcher routine that can be used to iterate whatever directory structure and fire events on matches or exclusions.  Of course if you want do to wildcard exclusions or directory exclusions you'd need to expand the capabilities of the class, but this should give you the basic idea of how to create what you need for now and how to make something more robust for the future.  I think I may rework it for my own purposes as well.
0
 

Author Closing Comment

by:linuxrox
ID: 39172173
thanks!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question