使用.NET System.IO File.Copy按顺序复制文件

时间:2016-03-31 13:55:38

标签: .net vb.net

我在Windows应用程序中创建基本文件复制操作。我注意到System.IO File.Copy随机复制文件。 有没有办法控制首先应该复制哪些文件。例如,如果我们要复制从最小文件大小到最大文件大小的文件。或按字母顺序,让我们说开始复制文件名为[从A到Z开始],或按文件名[从] 1到100的数字顺序。

我使用这个简单的代码从文件夹中复制文件,但这会随机复制文件"。见下文:

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
    Dim source as string = "c:\copyfiles"
    Dim destination as string = "d:\backup"
    Dim filepath as string = destination & "\"

        For Each filename As String In Directory.GetFiles(source)
            If File.Exists(filename) Then
                Dim dFile As String = String.Empty
                Dim dFilePath As String = String.Empty

                dFile = Path.GetFileName(filename)  'get filenames from source
                dFilePath = filepath & dFile  'concatenate filepath and filename

                File.Copy(filename, dFilePath, True) 'copy files from "c:\copyfiles" folder to destination
            End If

        Next
        MsgBox("Copy Successful", vbOKOnly, "Message")
End Sub

4 个答案:

答案 0 :(得分:3)

如果您确实希望通过名称(smallest to largest file size,或者最新或最旧)以外的其他方式处理它们,那么您应该使用DirectoryInfo,这样您就可以获得FileInfo个属性。

' simple ordering by size
Dim dix As New DirectoryInfo(_your_file_path)
For Each f As FileInfo In dix.EnumerateFiles.
               OrderByDescending(Function(o) o.Length)
    ' do stuff
Next

如果您认为自己可能还需要过滤器(即自上次运行以来只复制文件)那么EnumerateFiles而不是GetFiles()使用某些linq会更有效。在这种情况下,.NET将评估您的过滤器,并仅返回与您的过滤器匹配的过滤器而不是所有过滤器,以便您在代码中手动排除:

' process only TXT files in order of size
For Each f As FileInfo In dix.EnumerateFiles.
                Where(Function(w) w.Name.EndsWith(".txt")).
                OrderByDescending(Function(o) o.Length)
    ' do stuff
Next

答案 1 :(得分:1)

而不是在Directory.GetFiles()中使用foreach,而是将结果导入List并对该列表进行排序。如果要基于其他值进行排序,请使用FileInfo数据块检索文件信息并根据这些值进行排序。

使用排序列表,然后使用Foreach迭代它。列表<>保证提供一个迭代器,按照插入顺序返回列表中的项目。

 public void GetOrderedFiles()
{
    // Get unsorted list of file names
    List<string> fileNames = System.IO.Directory.GetFiles(strPath);

    List<System.IO.FileInfo> fileInformationList = new List<System.IO.FileInfo>();
    // For each file name, get a full file information block 
    fileNames.ForEach(fileName => fileInformationList.Add(new System.IO.FileInfo(fileName)));
    // Order by CreationTime. Could be any FileInfo data item.
    IOrderedEnumerable<System.IO.FileInfo> sortedFileInformation = fileInformationList.OrderBy(item => item.CreationTime);

    // Iterate round the sorted collection
    foreach(System.IO.FileInfo fileInformation in sortedFileInformation)
    {
        System.IO.File.Copy(fileInformation.FullName, /* Destination file name */)
    }
}

答案 2 :(得分:1)

您需要先对它们进行排序

For Each filename As String In Directory.GetFiles(source)

将以上行更改为:

Dim filenames() as String
filenames = filenames.OrderBy(Function(f) f.CreationTime) //by time
filenames = filenames.OrderBy(Function(f) f) //alphabetical, not sure of that one
filenames = filenames.OrderBy(Function(f) New FileInfo(f).Length) // by size
    For Each filename As String In filenames

答案 3 :(得分:1)

按名称排序:

For Each filename As String In Directory.GetFiles(source).OrderBy(Function(f) f)

按大小排序:

For Each filename As String In Directory.GetFiles(source).OrderBy(Function(f) New FileInfo(f).Length)