C# 文件副本(如果存在)轻微扭曲

C# 文件副本(如果存在)轻微扭曲,c#,.net,powershell,xcopy,C#,.net,Powershell,Xcopy,我有一个源文件夹FolderSource,其中包含过去6个月创建的200K个文件。每天都会将新文件(大约10k到11k)添加到此文件夹中 如果在过去30天内创建的文件不存在于FolderArchiveA,FolderArchiveB,FolderArchiveC 这是我的算法 1. Get one Filename from SourceFolder where CreatedDate > CurrentDate - 30 2. If Filename Exists in FolderAr

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

如果在过去30天内创建的文件不存在于
FolderArchiveA
FolderArchiveB
FolderArchiveC

这是我的算法

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更高效地编写此代码?

在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
    }
}

可能有更快的方法,但这种方法会起作用。

在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
    }
}

可能有更快的方法,但这种方法很容易奏效。

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