powershell - 如何使用PowerShell删除目录中的所有文件以及小于100kb的所有子目录
问题描述
我有一个包含数千个子目录的目录,我试图删除所有小于 100 kb 的文件。我写了以下脚本;但是,它会删除子目录,而不是删除其中的单个文件。
#root directory
$dir = "D:\S3\images"
#minimum size for file
$minSize = 100
#go through every item in the root directory
Get-ChildItem -Path $dir -Recurse | ForEach-Object {
#check if file length is less than 100kb
if ($_.Length / 100kb -lt $minSize) {
Remove-Item $_ -Force
} else {
#file is too big to remove
}
}
我究竟做错了什么?
解决方案
我通过以下修复更正了脚本:
#root directory
$path = "D:\S3\images"
Get-ChildItem -Path $path -Include *.* -File -Recurse | ForEach-Object {
#check if file length is less than 100kb
if ($_.Length -lt 100kb) {
Remove-Item $_ -Force
} else {
#file is too big to remove
}
}
推荐阅读
- linux - 如何更改远程构建机器 Visual Studio 2017
- json - 使用 Twig 或 Grunt 从 JSON 数组创建导航
- javascript - HTML - 单击按钮获取表格单元格值
- gitlab - 如何从 gitlab CI 管道中推送到仓库?
- angular - 路由更改时服务值变空 - Angular 5
- reactjs - React:将 JSX 作为函数参数传递
- c# - NHibernate 映射错误([EntityName] 未映射)
- mysql - Node.js:如何从 axios get 请求中读取参数?
- python - 滚动 groupby 导致 pandas 的双索引
- aspectj - 如何从触发 Aspect 方法调用中排除类