Solved

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

Posted on 2013-05-15
4
1,201 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

10 Experts available now in Live!

Get 1:1 Help Now