6

我正在尝试删除除 10 个最近的目录之外的所有目录,始终不包括 java 目录。不幸的是,它也删除了“java”目录的最后 10 个内容之外的所有内容。

我一直在尝试从以下链接修改解决方案,以使我的情况正常工作: 保留 x 个文件并删除所有其他文件 - 第二部分

目录结构如下:

dev_app_backup\java
dev_app_backup\2012-05-09_01-00-05_commnXsl  (contains Xsl files)
dev_app_backup\2012-05-09_01-00-05_published  (contains zip files)
dev_app_backup\various-dates-time_commonXsl
dev_app_backup\various-dates-time_published

我的计划是运行第二个脚本来清除 java 子目录。

#----- define folder where files are located ----#
$TargetFolder = "\\test\TestShare\dev_app_backup\*"

#----- number of directories to keep ----#
$keep = 10

#----- get zip files based on lastwrite filter ---#
$files = Get-Childitem $TargetFolder -recurse -exclude java

if ($files.Count -gt $keep) 
{
$files | Sort-Object -property $_.LastWriteTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force
}
4

1 回答 1

4

好的,这就是我想出的脚本,对不起,这是我的第一个powershell脚本......

$TargetFolder = "\test\TestShare\dev_app_backup*"
$keep = 10
$folders = Get-Childitem $TargetFolder -exclude java*
echo "folders"
echo $folders
echo "endfolders"
echo $folders.Count
echo $folders | Sort-Object -property $_.LastWriteTime | Select-Object -First ($folders.Count - $keep)
foreach ($folder in $folders | Sort-Object -property $_.LastWriteTime | Select-Object -First ($folders.Count - $keep))
{
echo $folder
$files = Get-Childitem $folder -recurse
echo $files
Remove-Item -Force $folder\$files
Remove-Item -Force $folder
}

回声只是为了帮助我调试,随时删除它们。

于 2012-05-10T18:25:22.847 回答