• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1160
  • Last Modified:

How do I copy all files from one folder to another folder - but only copy files that are newer to the 2nd folder

I need to use VB.net Visual Basic 2008.
I have 2 folders.
Each folder has a bunch of .jpg files.

How do I copy all the files from one folder to another folder -
and have it only copy the files that are NEWER to the 2nd folder?

c:\folder1\*.*
c:\folder2\*.*

Get all the file attributes and look at the Date modified for the attribute used to copy the folder.
Sometime there will be 2 files w/ the same name in both folders.
I want to update the folder2\*.* files with the newer ones from folder1\*.*
I am not trying to Sync the folders - just update Folder2.

I am new to VB - I think this is easy - but I am really having a hard time.
I know how to copy the folders w/ a dialog - but it will just prompt me to over write them.
I don't know how to use attribute
I think I have to build a list of the files in each folder - then compare the 2 lists - and only copy the newer ones.

Please help me.
0
CanvasYou
Asked:
CanvasYou
  • 6
  • 5
1 Solution
 
mkosbieCommented:
You can get access to the directories/files with System.IO.Directory, System.IO.DirectoryInfo, System.IO.File, and System.IO.FileInfo.  Here's a sample function that does what you want.
Imports System.IO
 
Module Module1
    Public Sub UpdateFolder(ByVal source As String, ByVal dest As String)
        Dim sDir As New DirectoryInfo(source)
        If Right(dest, 1).CompareTo("\") <> 0 Then
            dest = dest & "\"
        End If
 
        Dim sFile As FileInfo
        For Each sFile In sDir.GetFiles()
            Try
                sFile.CopyTo(dest & sFile.Name)
            Catch ex As IOException
                If sFile.LastWriteTime > File.GetLastWriteTime(dest & sFile.Name) Then
                    sFile.CopyTo(dest & sFile.Name, True)
                End If
            Catch ex As Exception
                'Other error occured
            End Try
        Next
    End Sub
End Module

Open in new window

0
 
CanvasYouAuthor Commented:
Thanks I will give it a try!
0
 
CanvasYouAuthor Commented:
What about memory Management?
Because there are about 1 million .jpg files in each folder -
and I need to copy 256 folders -
Is there a way to manage the memory so that it will clear the page pool or kernal (not sure of the term) after it copies each folder?
Is there also a way to have it show the copy progress dialog?

Or a way to have it count the number of files first - make that number = 100%
then as it copies each file count up - and figure the percentage?
So that another form opens while the copy is happening -
and has a "Please Wait.." Label
and a progressbar - that will show the progress?

Either way - the copy will take a long time - and I would like to show the user the progress.
Thanks in advanced.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mkosbieCommented:
The system should automatically manage the memory on a copy operation without a problem.  Each file is copied individually and goes out of scope immediately, meaning the memory can be freed by the system.

As far as a progress bar, I would use the System.Windows.Forms.ProgressBar class.  For each folder you plan to copy, set the Min value to 0, the Max to the number of files in the folder, and increment after each file.  A modified function might look like this.
    Public Sub UpdateFolder(ByVal source As String, ByVal dest As String)
        Dim sDir As New DirectoryInfo(source)
        If Microsoft.VisualBasic.Right(dest, 1).CompareTo("\") <> 0 Then
            dest = dest & "\"
        End If
 
        Dim files As FileInfo() = sDir.GetFiles()
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = files.Length
        ProgressBar1.Value = 0
        ProgressBar1.Step = 1
 
        Dim sFile As FileInfo
        For Each sFile In sDir.GetFiles()
            Try
                sFile.CopyTo(dest & sFile.Name)
            Catch ex As IOException
                If sFile.LastWriteTime > File.GetLastWriteTime(dest & sFile.Name) Then
                    sFile.CopyTo(dest & sFile.Name, True)
                End If
            Catch ex As Exception
                'Other error occured
            End Try
 
            ProgressBar1.PerformStep()
        Next
    End Sub

Open in new window

0
 
CanvasYouAuthor Commented:
I am pretty sure you hit the nail on the head -
I will have to test it -
I know what the progressbar object is - but I just was trying to figure out the calculation -
progressbar.maximum = total number of files in the folder
(example 100 files in a folder = 100%)
then after each file copies over - it will add 1%
but it will obviously not have 100 files - so it needs to calculate the total number of files
and be able to track what file it is on (while it is copying)
and then it updates the progress bar.

0
 
CanvasYouAuthor Commented:
I was already aware of what the progressbar object was. Thanks!
Just want the progressbar1 to actually track to progress of the files being copied over from source to dest.
using source as the 100% base..
ie: source has 100 files in its folder
as each file is either copied over (because it is newer, or it does not exist on the dest)- or is not copied (because it is older) - it will add 1% to the progressbar for each file.
Because there will be variable rate of files (ie there will not be 100 files exactly, each time).
So needs to count the total number of files in the folder - and make that ratio equal 100% of the progress bar.

Let me know what you think!
0
 
mkosbieCommented:
If you look in the updated function I gave you, you'll see this line:

ProgressBar1.Maximum = files.Length

"files" is an array of FileInfo objects representing the files in the directory passed in to the sub as "source".  The length of that array (files.Length) is the total number of files in the directory.

Then, inside the for each loop, you'll see this line after the file copy code:

ProgressBar1.PerformStep()

That line increments the progress bar by one (the step value) after each file is processed (copied or not).
0
 
CanvasYouAuthor Commented:
Oh okay I was not sure that - this was for "files" or for the length of a File
I was thinking it would progressbar based on each file as it copied based upon file size...
I see now that it is the actual count of files -
Cool - Thanks again!
0
 
CanvasYouAuthor Commented:
Worked Perfectly! Thank you!
0
 
mkosbieCommented:
Glad to help.
0
 
guthariusCommented:
Note:

To get the accepted solution to work for me in VB 2008 Express Edition I had to change the ProgressBar1 references to prepend the containing form name, i.e. Form1.ProgressBar1.

So for those literal types:

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = files.Length
        ProgressBar1.Value = 0
        ProgressBar1.Step = 1

For me became:

        Form1.ProgressBar1.Minimum = 0
        Form1.ProgressBar1.Maximum = files.Length
        Form1.ProgressBar1.Value = 0
        Form1.ProgressBar1.Step = 1
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now