powershell - 移动项目,但如果发生冲突则保持最新

标签 powershell

我有几个 foreach 循环,我在其中执行一些 if 和 elseif 循环。它们基本上设置为根据文件名整理出一堆文件。 但我遇到了一个特殊情况,其中有几个文件分别存在于 2 个不同的位置。但我想始终保留最新的文件。

我的代码是这样的

# Variable for destination folder
$CreoRod = "C:\Test\Creo"
# Variable for source folder
$97MappeRod = "C:\Test\97"
# Variable to get each item in source folder
$97Files = Get-ChildItem -File -Path $97MappeRod
# Variable to get each item in destination folder
$CreoRodFiles = Get-ChildItem -File -Path $CreoRod

# Foreach loop to move files from source folder to corresponding destination folder 
foreach ($file in $97Files) {
    if ($file -like "0*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\0xxx"
    } elseif ($file -like "48*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\48xxx"
    } elseif ($file -like "49*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\49xxx"
    } elseif ($file -like "97*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\97xxx"
    } elseif ($file -like "98*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\98xxx"
    } elseif ($file -like "99*") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\99xxx"
    } elseif ($file -notmatch "config.pro") {
        Move-Item -Path "$97MappeRod\$file" -Destination "$CreoRod\Diverse"
    }
}

是否可以做一些事情让它与现有文件进行比较,如果现有文件较新,则删除当前文件。但如果它较旧则更换?

最佳答案

我还没有测试过这个,但它可能对你的逻辑有帮助,首先你可以使用一个助手function 以测试目标中是否存在同名文件,如果存在,则测试是否应替换它($true 如果较新或目标没有包含该名称)或删除源文件($false)。

对于多个 if 条件,我建议您将它们替换为 switch statement这提供了更多的可读性。

function ShouldMove {
    param($File, $Destination)
    $testPath = Join-Path $Destination -ChildPath $File.Name
    if(Test-Path $testPath) {
        # if the destination folder contains this file name
        $destFile = Get-Item $testPath
        if($File.CreationTime -gt $destFile.CreationTime) {
            # if the source file is newer return `$true`
            return $true
        }
        # else `$false`
        return $false
    }
    # if destination does not have this file, return `$true`
    $true

}

foreach ($file in $97Files) {
    $destination = switch -Wildcard($file.Name) {
        "0*"  { "$CreoRod\0xxx" ; break }
        "48*" { "$CreoRod\48xxx"; break }
        "49*" { "$CreoRod\49xxx"; break }
        "97*" { "$CreoRod\97xxx"; break }
        "98*" { "$CreoRod\98xxx"; break }
        "99*" { "$CreoRod\99xxx"; break }
        { $_ -notmatch "config.pro" } { "$CreoRod\Diverse" }
        Default {
            Write-Warning "No condition was met, ignoring $($file.Name)"
        }
    }
    
    if(-not $destination) { continue }
    if(ShouldMove -File $file -Destination $destination) {
        # `-Force` to replace it
        Move-Item -LiteralPath $file.FullName -Destination $destination -Force
        continue
    }
    # if `ShouldMove` returned `$false` then, instead of moving
    # the file, delete the source
    Remove-Item -LiteralPath $file.FullName
}

关于powershell - 移动项目,但如果发生冲突则保持最新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71382982/

相关文章:

powershell - 如果工作完成了怎么办

asp.net - 在 Windows Server 2012 的 Azure 启动任务中安装功能

email - Powershell 打开带签名的电子邮件草稿

powershell - 获取返回的最后一个对象

powershell - PowerShell 的测试路径超时

azure - 用于修改 Azure Blob 存储中对象的 ContentType 的 Powershell 函数

powershell - 为什么我的 PowerShell 退出代码始终为 "0"?

vba - 从字符串变量分配 Excel 数字格式

PowerShell 3 参数

powershell - 为什么此命令不能与TFS构建一起执行?