Click here to Skip to main content
16,016,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an issue where at first we were copying the contects of this folder on form load that had several scripts in it to the local machine but over time that folder has grown where copying all thoses files at once is talking forever.

What i need help doing is changing the code were using now to check if the files has a newer date on it then the one on the local machine and if so then i need it to copy that file if not then just continue to check the other files on the machine.

the code im using now is
VB
Public Sub File_Check_Apps_USMC()
    Dim strSrc As String = "\\mcusquanfs01\NRFZHD\RDM_NRFK_WN7\SynergyLite\Static_Fixes\Application"
    Dim strDest As String = "C:\Program Files (x86)\Synergy\Remote_Fixes\Applications"
    Dim dirInfo As New System.IO.DirectoryInfo(strSrc)
    Dim fsInfo As System.IO.FileSystemInfo

    If Not System.IO.Directory.Exists(strDest) Then
        System.IO.Directory.CreateDirectory(strDest)
    End If

    For Each fsInfo In dirInfo.GetFileSystemInfos
        Dim strDestFileName As String = System.IO.Path.Combine(strDest,
        fsInfo.Name)

        If TypeOf fsInfo Is System.IO.FileInfo Then
            System.IO.File.Copy(fsInfo.FullName, strDestFileName, True)
            'This will overwrite files that already exist
        Else

        End If

    Next

End Sub


help would be great.
Posted

1 solution

First, you don't need the the If TypeOf fsinfo Is System.Io.FileInfo becuase you're getting nothing but FileInfo objects from the GetFileInfos call. I'm all for defensive coding but that's overkill.

Next, You don't need to put the namespaces ahead of every class, like System.Io.Path.Combine. If you put Imports statements at the top of your file, you don't need to type all that stuff. It just becomes:
Include System.Io
.
.
If Not Directory.Exists(...)

Dim destFileName As String = Path.Combine(...)


Oh, and putting the old Hungarian type prefix on everything is so 1990's. Stop it. Use more descriptive variable names, like destFilepath and sourceFilepath. That garbage was only used when the compiler didn't do type checking for you, such as VBScript. VB.NET is fully type safe. What you're doing is redundant.

Now, on to the problem at hand...

All you have to do is get a FileInfo object for the soruce file and the destination file, then compare the LastWriteTime properties of both. They're different, copy the file. If not, move on to the next file.
 
Share this answer
 
Comments
Zachary.shupp 2-Mar-12 21:15pm    
how would i do that for the 20 - 50 files i have in that folder without hard coding it? i understand what your saying and i am trying to correct my coding if you could give me an example of how it should look to compare the files and copy that would help alot. Thanks for talking your time to help me.
Dave Kreskowiak 2-Mar-12 23:36pm    
Hardcoding what?? You're already using a loop! You interate over the files in your source folder (which you're already doing), then you build the target path using the filename you already have, get a FileInfo object on that file and compare to the source file .LastWriteTime you already have. What's to hard code???
Zachary.shupp 3-Mar-12 7:07am    
sorry i have gotten thrown into this project because they just let go of our programmer and its all new to me. I see what your saying i think. but if you could post an example with using the .lastwritetime i would really appreciate it
Dave Kreskowiak 3-Mar-12 19:19pm    
Something like this:

For Each fsInfo In dirInfo.GetFileSystemInfos
Dim strDestFileName As String = System.IO.Path.Combine(strDest, fsInfo.Name)
Dim destFileInfo As New FileInfo(strDestFileName)

If fsInfo.LastWriteTime > destFileInfo.LastWriteTime Then
System.IO.File.Copy(fsInfo.FullName, strDestFileName, True)
End If
Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900