使用vb.net帮助仅复制日期较新的文件 [英] help copying only files that have a newer date on them with vb.net

查看:64
本文介绍了使用vb.net帮助仅复制日期较新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,首先我们要在窗体加载时将其中包含多个脚本的该文件夹的保护条件复制到本地计算机,但是随着时间的流逝,该文件夹已经扩大了,一次复制所有这些文件一直在讨论.

我需要帮助的是更改代码,这些代码现在正在使用中,以检查文件上是否有较新的日期,然后是本地计算机上的一个,如果是,那么我需要它来复制该文件(如果没有),然后继续检查另一个机器上的文件.

我现在使用的代码是

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

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.

推荐答案

首先,您不需要If TypeOf fsinfo Is System.Io.FileInfo,因为除了GetFileInfos的FileInfo对象外,您什么也没有称呼.我全都是防御性的代码,但这太过分了.

接下来,您不需要将名称空间放在每个类的前面,例如System.Io.Path.Combine.如果将Imports语句放在文件顶部,则无需键入所有内容.变成:
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(...)



哦,在所有内容上加上旧的匈牙利语前缀都是1990年代.停下来.使用更多描述性的变量名称,例如destFilepathsourceFilepath.仅当编译器不为您执行类型检查时才使用该垃圾,例如VBScript. VB.NET是完全类型安全的.您正在做的事是多余的.

现在,解决当前的问题...

您要做的就是获取soruce文件和目标文件的FileInfo对象,然后比较两者的LastWriteTime属性.他们是不同的,复制文件.如果不是,请移至下一个文件.



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.


这篇关于使用vb.net帮助仅复制日期较新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆