Reputation: 13
Hello everyone I have 1 folder and include many sub folders and many .txt
files.I want to delete .txt
files especially smaller than 10kb
.
I've tried this one but I got some errors every time.
$Dir = "C:\Users\*************\Desktop\test"
'$SizeMin' = 10 #KB
Get-ChildItem -Path $Dir -Recurse |
Where {$_.Length / 10KB -lt $SizeMin} |
Remove-Item -Force
"Unexpected token '$SizeMin' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken"
Upvotes: 1
Views: 4905
Reputation: 431
This code can help you, it removes files smaller than 10kb (10000 bytes) in a given directory:
$path = 'C:\Users\*************\Desktop\test'
Get-ChildItem $path -Filter *.stat -recurse |?{$_.PSIsContainer -eq $false -and $_.length -lt 10000}|?{Remove-Item $_.fullname -WhatIf}
Upvotes: 1
Reputation: 2415
Whenever learning something new, especially coding, I find it best to break everything down and take my time writing drawn out code first and compacting it later. You could use and edit the following drawn out code to get a better understanding as to what is happening:
#Root directory
$dir = "C:\Users\*************\Desktop\test"
#Minimum size for file
$minSize = 10
#Throwing through every item in root directory
Get-ChildItem -Path $dir -Recurse | ForEach-Object{
#Check if file length if less than 10
if ($_.Length / 10KB -lt $minSize){
Remove-Item $_ -Force
}else{
#File is too big to remove
}
}
Upvotes: 4