Solved

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

Posted on 2013-05-15
4
1,245 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 500 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 45
Resolve Dependency Issues 4 44
Run time Error 4 34
Ajax Panel Not Updating When Triggered By Timer 4 40
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now