我有包含数千个子目录的目录,我正在尝试删除所有小于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
}
}
我究竟做错了什么?
最佳答案
您的长度检查不正确,您不需要进行除法。另外,您可能想使用Get-ChildItem
参数跳过-File
中的目录。
尝试:
Get-ChildItem -File -Path $dir -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
}
}
关于powershell - 如何使用PowerShell删除目录中的所有文件和所有小于100kb的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63471130/