【问题标题】:File Copy if exists with a slight twist文件复制(如果存在)稍微扭曲
【发布时间】:2017-11-21 23:58:53
【问题描述】:

我有一个源文件夹FolderSource,其中包含在过去 6 个月内创建的 200K 文件。每天都会向此文件夹添加新文件(大约 10k 到 11k)。

如果最近 30 天内创建的文件在 FolderArchiveAFolderArchiveBFolderArchiveC 中不存在,我需要将它们复制到 FolderDestination

这是我的算法

1. Get one Filename from SourceFolder where CreatedDate > CurrentDate - 30
2. If Filename Exists in FolderArchiveA go to step 6
3. If Filename Exists in FolderArchiveB go to step 6
4. If Filename Exists in FolderArchiveC go to step 6
5. Copy File defined in FileName to FolderDestination
6. If there are more files to be processed, go to Step 1

我用 C# 编写了这个,但我要使用 FBAF(想想 RBAR,但带有文件)。执行需要 40 多分钟。

还有其他方法可以让我使用 Powershell 或 XCopy 更有效地编写代码吗?

【问题讨论】:

  • 很容易。查找Get-ChildItemWhere-Object。它利用了System.IO.FileInfoSystem.IO.DirectoryInfo .NET 类。
  • 您是否检查过流程的哪一部分具体是瓶颈?您可能只是受到磁盘 i/o 的限制。
  • @RajMore,如果这是一个长时间运行的服务,我会将所有文件名加载到内存中,添加一个文件系统观察器来维护它(以及定期捕获所有解析器)并在那个。
  • 如果FolderDestination中已经存在文件怎么办?
  • 如果文件已经存在,覆盖

标签: c# .net powershell xcopy


【解决方案1】:

在 PowerShell 中快速组合:

#Requires -Version 3

$FolderSource = Get-ChildItem -Path 'C:\Source' -File |
    Where-Object { $PSItem.CreationTime -gt (Get-Date).AddDays(-30) }
$FolderDestination = 'C:\Destination'
$Archives = @((Get-ChildItem -Path @('C:\ArchiveA','C:\ArchiveB','C:\ArchiveC') -File |
    Select-Object -ExpandProperty 'Name').ToUpper() |
    Sort-Object |
    Get-Unique)

ForEach ($File in $FolderSource)
{
    If ($File.Name -notin $Archives)
    {
        $File | Move-Item -Destination $FolderDestination
    }
}

可能有更快的方法,但这种方法行得通

【讨论】:

    猜你喜欢
    • 2011-09-11
    • 2015-04-19
    • 1970-01-01
    • 2019-03-17
    • 2020-02-02
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多