Я думаю, что это должно сделать это:
$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
Я сократил код, чтобы получить файлы, отфильтровать их по возрасту и (не) существованию в стоковой папке и немного их удалить. Возможно, это делает код немного менее читабельным ..?