【问题标题】:Powershell - Copy files only from the current day and if they don't exist in the target directoryPowershell - 仅从当天复制文件,如果它们不存在于目标目录中
【发布时间】:2023-03-08 11:44:01
【问题描述】:

我正在尝试开发一个脚本,将 .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 语句。

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

【问题讨论】:

  • 为什么不用Robocopy 来代替?它有很多选项和功能。

标签: powershell file date directory copy


【解决方案1】:

仅使用 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
        }
}

【讨论】:

  • @VictorLeclerc 对不起,我的错...-Filter 应该是-Include。如果要复制的所有文件都是 .txt 文件,我还添加了一个额外的内联注释来调整模式。
  • @VictorLeclerc 你能在定义变量$notThese 的行之后添加Write-Host $notThese 吗?它应该显示一个字符串archive|IDISC\ Test|save|archive_B2B|archive_tmp|Errors。我当然看不到任何真实的文件名,但也许您应该使用"*avail*.txt", "*items*.txt", "*MITEM*.txt","*BPRIC*.txt", "*CAPRO*.txt" 之类的模式,因为它们可能并非都以这些关键字中的任何一个开头?
  • @VictorLeclerc 您能否检查预期要复制的文件是否确实是今天最后一次修改的? (这是 Where-Object 子句中的第二个要求)
  • 确实,你是对的!我检查了sources文件夹,除了avail文件,当天没有其他类型的文件。我用一个 MITEM 进行了测试,它被转移到了目标目录。谢谢
  • @VictorLeclerc 啊,很高兴听到这个消息!忙于测试,但找不到任何其他原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2019-12-25
  • 2010-12-04
  • 2020-08-30
  • 1970-01-01
  • 2018-01-10
相关资源
最近更新 更多