Powershell - 仅从当天复制文件,如果它们不存在于目标目录中

Powershell - Copy files only from the current day and if they don't exist in the target directory

我正在尝试开发一个脚本,将 .txt 文件从两个源文件夹复制到一个目标文件夹。

我还想添加 2 个附加条件:

  1. 如果目标目录中已有文件,则跳过
  2. 复制的文件不能早于当天

这是我做的,但没有达到预期效果:

$SourceDirItemsMITEMAvail = "\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\product\*"
$SourceDirCaproBPRIC = "\wipfs02\Data\webshop\test\hybris_import\carhartt\product\*"
$TargetDirB2B = "\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\b2b\product\"

$ExcludeSubDir = "archive", "IDISC Test", "save", "archive_B2B", "archive_tmp", "Errors"

# Copy only the specific files
$SpecificTypesEcomCloudImport = "avail*", "items*", "MITEM*"
$SpecificTypesHybrisImport = "BPRIC*", "CAPRO*"

# Get the current date
$CurrentDateFiles = (Get-Date).Date

# Copy items from source directories and transfer them to the B2B Folder
For ($i=0; $i -lt $SourceDirItemsMITEMAvail.Count -and $SourceDirCaproBPRIC.Count; $i++) {

    #  If the copied files don't already exist into the target directory, then copy them
    if (-not(Test-Path -Path $SourceDirItemsMITEMAvail, $SourceDirCaproBPRIC)) {

    try {
        # The copied files must not be older that the current day
        $CopyFilesEcomCloudImportDir = Copy-Item $SourceDirItemsMITEMAvail -Exclude $ExcludeSubDir -Destination $TargetDirB2B -PassThru -Verbose -Include $SpecificTypesEcomCloudImport | Where-Object { $_.LastWriteTime -eq $CurrentDateFiles}
        Write-Host ""$CopyFilesEcomCloudImportDir.Count" file(s) have been copied from $SourceDirItemsMITEMAvail to $TargetDirB2B."

        # The copied files must not be older that the current day
        $CopyFilesHybrisImportDir = Copy-Item $SourceDirCaproBPRIC -Exclude $ExcludeSubDir -Destination $TargetDirB2B -PassThru -Verbose -Include $SpecificTypesHybrisImport | Where-Object { $_.LastWriteTime -eq $CurrentDateFiles}
        Write-Host ""$CopyFilesHybrisImportDir.Count" file(s) have been copied from $SourceDirCaproBPRIC to $TargetDirB2B."
    }
    catch {
        "Error: $($_.Exception.Message)"
    }
    }else {
        Write-Host "Cannot copy $CopyFilesEcomCloudImportDir because a file with that name already exists in the destination folder."
        Write-Host "Cannot copy $CopyFilesHybrisImportDir because a file with that name already exists in the destination folder."
    }
}

要么从源目录复制所有文件,要么立即进入 else 条件。

如果我删除 if (Test-Path) 条件,它会从源复制所有文件,甚至是那些早于当天的文件。

如果我让 if (Test-Path) 条件成立,它会立即转到 else 语句。

你能帮我解决这个问题吗?谢谢!

仅使用 PowerShell,您可以这样做:

$SourceDirItemsMITEMAvail = "\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\product"
$SourceDirCaproBPRIC      = "\wipfs02\Data\webshop\test\hybris_import\carhartt\product"
$TargetDirB2B             = "\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\b2b\product"
$ExcludeSubDir            = "archive", "IDISC Test", "save", "archive_B2B", "archive_tmp", "Errors"

# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($ExcludeSubDir | ForEach-Object { [Regex]::Escape($_) }) -join '|'

# Copy only the specific files
##############################
# if all files are .txt files, change the patterns below to include that:
# like "avail*.txt", "items*.txt", "MITEM*.txt","BPRIC*.txt", "CAPRO*.txt"
##############################
$FileNamePattern = "avail*", "items*", "MITEM*","BPRIC*", "CAPRO*"

# Get the current date
$today = (Get-Date).Date

Get-ChildItem -Path $SourceDirItemsMITEMAvail, $SourceDirCaproBPRIC -Include $FileNamePattern -File -Recurse | 
    Where-Object { $_.DirectoryName -notmatch $notThese -and $_.LastWriteTime.Date -eq $today } |
    ForEach-Object { 
        $targetFile = Join-Path -Path $TargetDirB2B -ChildPath $_.Name
        if (Test-Path -Path $targetFile -PathType Leaf) {
            Write-Host "File '$targetFile' already exists. Skipping"
        }
        else {
            $_ | Copy-Item -Destination $TargetDirB2B -Force
        }
}