?
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
Medium Priority
?
1,159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 8

Accepted Solution

by:
mkosbie earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

801 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