我正在编写一个 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}