Solved

how to copy a directory and bypass errors VB 08

Posted on 2008-11-02
5
310 Views
Last Modified: 2013-11-26
I am trying to make a small backup program in VB 2008 that copies a directory.  I have researched for a long time and have posted a previous question with no suitable answer.

The problem I am running into is if I try to copy a directory and a file is in use or I have access denied it halts the copying progress at the file with the error and doesn't copy anything after it.  I have explained this to many people but a lot of them can't understand the question so I am going to write an example:

say c:\test contains the following files:
a.txt
b.txt
c.txt

Say we do not have access permissions to b.txt

If i try to copy the directory using:
My.Computer.FileSystem.CopyDirectory("c:\test", "c:\backup", True)

I will get an access denied error on b.txt and the copying process halts. I basically need the program to do something like this:

copydirectory c:\test
if file error Then
Skip file and continue copying

Easier Said then done.  Please, do not focus on fixing the error by correcting permissions.  That is not the solution I am looking for.  I basically, want to skip any errors and continue copying until the directory and sub directories are copied.
If anyone has any suggesions, I would greatly appreciate it.
0
Comment
Question by:inimicaljords
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
mkosbie earned 500 total points
ID: 22864931
You could implement your own CopyDirectory routine that ignores errors.  Here's an example (I didn't put in a UI, you could if you wanted).  Note that FileInfo and DirectoryInfo are in the System.IO namespace.
    Private Sub CopyDirectory(ByVal SourcePath As String, ByVal DestinationPath As String)
        If Not DestinationPath.EndsWith("\") Then
            DestinationPath = DestinationPath & "\"
        End If
 
        Dim SourceDirectory As DirectoryInfo = New DirectoryInfo(SourcePath)
        Dim DestinationDirectory As DirectoryInfo = New DirectoryInfo(DestinationPath)
        If Not DestinationDirectory.Exists Then DestinationDirectory.Create()
 
        For Each SourceDirectorySubdir As DirectoryInfo In SourceDirectory.GetDirectories()
            CopyDirectory(SourceDirectorySubdir.FullName, DestinationPath & SourceDirectorySubdir.Name)
        Next
 
        For Each SourceDirectoryFile As FileInfo In SourceDirectory.GetFiles()
            Try
                SourceDirectoryFile.CopyTo(DestinationPath & SourceDirectoryFile.Name)
            Catch ex As Exception
                'Do nothing on error
            End Try
        Next
    End Sub

Open in new window

0
 
LVL 21

Expert Comment

by:mastoo
ID: 22867639
Might use Process class to invoke XCopy.  It has a lot more flexibility, and includes /C to continue copying after an error.
0
 
LVL 1

Author Comment

by:inimicaljords
ID: 22867851
this looks very promising but I am having a little problem.
CopyDirectory("c:\test", "c:\backup")

When I call the function I get an error:
DestinationDirectory.Create()
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

I tried putting a msgbox in before the line code for debugging and I get an error on the msg box
MsgBox(DestinationDirectory)
Argument 'Prompt' cannot be converted to type 'String'

Seems like destinationdirectory and sourcedirectory can't convert DirectoryInfo into a string.

I extremely appreciate the help.  
0
 
LVL 1

Author Closing Comment

by:inimicaljords
ID: 31512584
Thank you so much!  I fixed the error I was having.  It was with my part which was causing an infinite loop.  Your code worked perfectly.  I appreciate it more than you can imagine =)
0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22873610
Glad to help.
0

Featured Post

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

831 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