【问题标题】:PowerShell - Move specific files from source sub folders to destination with identical sub folder structurePowerShell - 将特定文件从源子文件夹移动到具有相同子文件夹结构的目标
【发布时间】:2012-07-19 23:12:20
【问题描述】:

我正在编写一个 PS 脚本,按文件类型将 5 个最新文件从源目录移动到目标目录,同时保留子文件夹结构。例如,我想将任何以 AB_ 开头的文件从 ParentFolder1\FolderA、ParentFolder1\FolderB、ParentFolder1\FolderC 等移动到 ParentFolder2\FolderA、ParentFolder2\FolderB、ParentFolder2\FolderC。

父文件夹1

--文件夹A

----AB_1234.txt

----AB_5678.txt

----XY_9876.txt

----XY_9876.txt

--文件夹B

----AB_1234.txt

----AB_5678.txt

----XY_9876.txt

----XY_9876.txt

--文件夹C

----AB_1234.txt

----AB_5678.txt

----XY_9876.txt

----XY_9876.txt

有没有办法将它绑定到 for 循环中?这是我目前所拥有的:

$source = "C:\Test"
$dest = "E:\archive"
$AB = Get-ChildItem -Path $source -include AB_* -Recurse | Where-Object {-not $_.PsIsContainer}
$XY = Get-ChildItem -Path $source -include XY_* -Recurse | Where-Object {-not $_.PsIsContainer}
$keep = 5
$logfile = "E:\_archive\temp\log_{0:MM-dd-yyyy}.log" -f (Get-Date)

if ($AB.Count -gt $keep) {
    $AB | Sort -property LastWriteTime | Select-Object -First ($AB.Count - $keep) | Move-Item -destination $dest\AB -Force -Verbose
    }
Else
    {"No AB_ files to archive." | Out-File $LogFile -Append -Force}

if ($XY.Count -gt $keep) {
    $XY | Sort -property LastWriteTime | Select-Object -First ($XY.Count - $keep) | Move-Item -destination $dest\XY -Force -Verbose
    }
Else
    {"No XY_ files to archive." | Out-File $LogFile -Append -Force}

【问题讨论】:

    标签: powershell


    【解决方案1】:
    function Move-Newest-Files 
    {
        param (
            [parameter(Mandatory = $true)] [string] $source,
            [parameter(Mandatory = $true)] [string] $destination,
            [parameter(Mandatory = $true)] [array] $filter,
            [parameter(Mandatory = $false)][int] $keep = 5
        )
    
        try
        {
            $source = Resolve-Path $source
    
            $list = Get-ChildItem -Path $source -Include $filter -Recurse -Force |
                Where-Object { -not $_.psIsContainer } |
                Sort-Object -property LastWriteTime -descending | 
                Select-Object -First $keep
    
            foreach ($file in $list)
            {
                $destfile = $file -replace [regex]::Escape($source), $destination
                $destpath = Split-Path $destfile -Parent
    
                if (!(Test-Path $destpath))
                    { $null = New-Item -ItemType Container -Path $destpath }
    
                Move-Item $file -Force -Destination $destfile
            }
        }
    
        catch
        {
            Write-Host "$($MyInvocation.InvocationName): $_"
        }
    }
    
    Move-Newest-Files . "D:\xyzzy" @("*.cs", "*.txt", "*.exe") 10
    

    【讨论】:

    • 谢谢大卫。能否请您为我定义 $filter 和 $file 变量?
    • $filter 是一个数组构建,其中包含您要保留的文件的文件扩展名:类似于 @(".cs", ".txt", ".exe") 或这个@(".*")
    【解决方案2】:

    使用Robocopy 可能会更好。

    【讨论】:

    • 我没有使用 xcopy 或 robocopy 的主要原因是我找不到保留最新的 5 个文件并移动其余文件的方法。有没有办法用 Robocopy 做到这一点?
    • 抱歉,我没有注意到最新的 5 个文件。