Link to home
Start Free TrialLog in
Avatar of CanvasYou
CanvasYou

asked on

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.
Avatar of mkosbie
mkosbie

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

Avatar of CanvasYou

ASKER

Thanks I will give it a try!
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.
ASKER CERTIFIED SOLUTION
Avatar of mkosbie
mkosbie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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!
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).
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!
Worked Perfectly! Thank you!
Glad to help.
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