移动不等于库存文件夹中文件且早于X的文件

时间:2019-05-08 14:53:13

标签: powershell

我正在尝试将文件从一个文件夹(source)移出到备份位置,该文​​件不在另一个文件夹(stock files中,并且源文件已超过30天。

我尝试了几次,但似乎无法正确完成。

$days = 30 
$Source = "G:\fs01\" 
$Destination = "G:\fso2\" 
$StockFileLocation = "g:\fso1Stock\" 
$FileExtention = ".mpr" 
$limit = (Get-Date).AddDays(-$days) 
$SourceFiles = (Get-ChildItem $Source | where {$_.extension -eq $FileExtention} | % {$_.Name}) 
$StockFiles = (Get-ChildItem $StockFileLocation | where {$_.extension -eq $FileExtention} | % {$_.Name})

目标:针对源文件中的每个文件。
如果源中的文件名<>在“库存文件位置”中的文件名和
源超过30天,然后将文件移至目标。

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的要求,则可以在单个管道中完成

$days = 30 
$Source = "G:\fs01\" 
$Destination = "G:\fso2\" 
$StockFileLocation = "g:\fso1Stock\" 
$FileExtention = "*.mpr" 
$limit = (Get-Date).Date.AddDays(-$days)

Get-ChildItem -Path $Source -Filter $FileExtention | 
    Where-Object {$_.LastWriteTime -lt $Limit -and
      (!(Test-Path ($_.FullName -replace [Regex]::Escape($Source),$StockFileLocation)))}|
        Move-Item $Destination -WhatIf

如果输出看起来正常,请删除结尾的-WhatIf

答案 1 :(得分:1)

我认为应该这样做:

$days = 30 
$Source = "G:\fs01\" 
$Destination = "G:\fso2\" 
$StockFileLocation = "g:\fso1Stock\" 
$limit = (Get-Date).AddDays(-$days) 

# get an array of filenames already in the $StockFileLocation folder (names only)
$StockFiles  = Get-ChildItem -Path $StockFileLocation -Filter '*.mpr' -File | Select-Object -ExpandProperty Name

# get an array of FileInfo objects of .mpr files older that $limit and loop through
$SourceFiles = Get-ChildItem -Path $Source -Filter '*.mpr' -File | Where-Object { $_.LastWriteTime -lt $limit }
$SourceFiles | ForEach-Object {
    if ($StockFiles -notcontains $_.Name) {
        $_ | Move-Item -Destination $Destination -WhatIf
    }
}

对结果满意后,请移除-WhatIf开关


编辑

根据您的注释中的要求,在包装在函数中的相同内容下方:

function Move-OldNotInStock {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [Alias('Path')]
        [string[]]$Source,                               # Specifies a path to one or more source locations. Wildcards are accepted.

        [Parameter(Mandatory = $true, Position = 1)]     # The destination folder. Will be created if it does not exist
        [string]$Destination,

        [string]$StockFileLocation = "G:\fso1Stock" ,    # The folder for the files 'in stock'. defaults to "G:\fso1Stock" 
        [int]$AgeInDays = 30,                            # The number of days to consider files old. Defaults to 30
        [string]$FilePattern = '*.mpr',                  # The file pattern of files to collect. Defaults to '*.mpr'
        [switch]$Recurse,                                # Whether or not subfolders in the source path(s) should be included 
        [switch]$WhatIf                                  # 'Chicken switch'. Allows to run the function without actually moving files.
    )    

    # create the destination path if it does not exist
    if (!(Test-Path -Path $Destination -PathType Container)) {
        Write-Verbose "Creating folder '$Destination'"
        New-Item -Path $Destination -ItemType 'Directory' -Force | Out-Null
    }

    $limit = (Get-Date).AddDays(-$AgeInDays)

    # get an array of filenames already in the $StockFileLocation folder (names only)
    $StockFiles  = Get-ChildItem -Path $StockFileLocation -Filter $FilePattern -File | Select-Object -ExpandProperty Name

    # get an array of FileInfo objects of .mpr files older that $limit and loop through
    Get-ChildItem -Path $Source -Filter $FilePattern -File -Recurse:$Recurse | 
        Where-Object { $_.LastWriteTime -lt $limit -and $StockFiles -notcontains $_.Name } |
        Move-Item -Destination $Destination -WhatIf:$WhatIf
}

以这种方式接受-StockFileLocation-AgeInDays-FilePattern的默认值

Move-OldNotInStock -Source "G:\fs01" -Destination "G:\fso2" -WhatIf

我已经缩短了获取文件的代码,根据文件存入的年龄和(不)存在情况对其进行过滤,然后将其删除了一些。也许这会使代码的可读性降低。.?