Solved

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

Posted on 2008-10-18
12
1,145 Views
Last Modified: 2013-11-27
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
Comment
Question by:CanvasYou
  • 6
  • 5
12 Comments
 
LVL 8

Expert Comment

by:mkosbie
ID: 22751122
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
 

Author Comment

by:CanvasYou
ID: 22751396
Thanks I will give it a try!
0
 

Author Comment

by:CanvasYou
ID: 22751409
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Accepted Solution

by:
mkosbie earned 500 total points
ID: 22751450
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
 
LVL 8

Expert Comment

by:mkosbie
ID: 22751452
0
 

Author Closing Comment

by:CanvasYou
ID: 31507511
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
 

Author Comment

by:CanvasYou
ID: 22751482
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
 
LVL 8

Expert Comment

by:mkosbie
ID: 22752832
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
 

Author Comment

by:CanvasYou
ID: 22752896
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
 

Author Comment

by:CanvasYou
ID: 22753302
Worked Perfectly! Thank you!
0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22753743
Glad to help.
0
 
LVL 1

Expert Comment

by:gutharius
ID: 22857839
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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