Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a backup of a folder, what I'm having problem with is if the backup folder already has files that exist in the original folder how do I get it to move onto the next file without over writing?

What I have tried:

I'm currently having to over write the existing files using the code below.

VB
My.Computer.FileSystem.CopyDirectory(TextBox5.Text + "\item\ModelItem\Mesh\", TextBox5.Text + "\item\ModelItem\BACKUP\", True)


The full code if you need to know or some help on how I can make it a little nicer. I know it's ugly but I'm new to this and just need simplicity which usually equates to ugly code for me :)

VB
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


    ' Does a check to make sure Simple Loot Filter is pointed to the correct folder
    If My.Computer.FileSystem.DirectoryExists(TextBox5.Text + "\item\ModelItem\Mesh\") AndAlso My.Computer.FileSystem.FileExists(TextBox5.Text + "\RF.exe") Then

        ' This just changes the property of some buttons that are no longer required once the previous statement is true
        Button3.BackColor = Color.Lime
        TextBox5.Enabled = False
        Button4.Enabled = False
        TextBox6.Enabled = False

        MessageBox.Show("Everything Is in the right place, now load RF And once on the server selection screen make your loot choice.")

        'If no BACKUP folder found then it will create one and produce a message saying one is being created
        If Not My.Computer.FileSystem.DirectoryExists(TextBox5.Text + "\item\ModelItem\BACKUP\") Then
            My.Computer.FileSystem.CopyDirectory(TextBox5.Text + "\item\ModelItem\Mesh\", TextBox5.Text + "\item\ModelItem\BACKUP\")
            MessageBox.Show("There is no backup of your files, creating one now.")

            'Backups the Mesh folder to BACKUP, want to add a message saying if this was needed or all files were already backed up.
        Else
            My.Computer.FileSystem.CopyDirectory(TextBox5.Text + "\item\ModelItem\Mesh\", TextBox5.Text + "\item\ModelItem\BACKUP\", True)

        End If

        ' If the files that don't exist for this to work then it will display this message.
    Else Button3.BackColor = Color.Red
        MessageBox.Show("Something appears to be wrong, direct this app to your '\RF Online' directory then click the check button.")



    End If

End Sub
Posted
Updated 7-Feb-21 14:04pm
v2

1 solution

Hi,

As I was unfamiliar with the method you used, I read the documentation: FileSystem.CopyDirectory Method (Microsoft.VisualBasic.FileIO) | Microsoft Docs[^] .
I suggest so do you.

:)
 
Share this answer
 
Comments
Ricci Parker 7-Feb-21 19:49pm    
I have read all the documentation I could find on this including the one you suggested. If you had tried to use any of the methods it states including the one I'm using here then you will notice all of them write over any files you have in the destination folder.
If using this method for a very large amount or large sized files then this would become a very slow way of doing things. I want to be able to skip any files that already exist in the destination folder an only copy files that don't.

If there is something you have seen in that documentation that I am not then please! share it with me.

When answering a question please:
Read the question carefully.

Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.

Let's work to help developers, not make them feel stupid.
Luc Pattyn 7-Feb-21 21:06pm    
The third parameter is a boolean flag named overwrite; you pass True and complain files get overwritten. Have you tried setting it False?

Ricci Parker 7-Feb-21 21:33pm    
Yes and that is where I run into the problem, when it is set to False the function stops at the first file it copied, if there are more still to move it will not move them.
Luc Pattyn 7-Feb-21 21:54pm    
When the available methods don't satisfy your requirements, you'll have to create your own. I would suggest you use Directory.EnumerateFiles, and for each file returned, you check its existence in the destination, and copy when appropriate using File.Copy.

Beware, whatever I/O methods you use, you should always set up a try-catch construct and handle possible Exceptions (at least log or display the entire Exception.ToString() result). Doing so would also have indicated why FileSystem.CopyDirectory didn't perform what you expected.

Finally, file path operations are best handled by the Path class; e.g. its Path.Combine method is quite useful.

PS: the FileSystem class is rather VB.NET specific, the classes and methods mentioned above are more general, they are the normal way to do file I/O in C#, but work equally well in VB.NET; anyway, you may have to read up some more...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900