Solved

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

Posted on 2013-05-15
4
1,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 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