Solved

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

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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