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,138 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now