Reputation: 23
I was wondering how I can display a list of empty files in a directory
$test = gci "C:\Users\Freedom\Documents" -Recurse
$test | Where-Object {$_.PsISContainer} | Select-Object FullName | Where-Object {$_.GetFiles() -eq 0}
I Don't understand because when I do get-childitem | get-member
I get a list of properties and methods I can use and in the list is getfiles()
why can't I use this method why's it giving me an error message?
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'GetFiles'.
Upvotes: 0
Views: 1216
Reputation: 355
Use Get-ChildItem
and the File
flag, -Recurse
is needed to get every file in the folder and in the folder below. Then get all the files were Get-Content
returns null.
Get-ChildItem $YourPath -Recurse -File | Where-Object {!(Get-Content $_.Fullname)}
Upvotes: 0
Reputation: 17472
try this
Get-ChildItem "c:\temp" -File -Recurse | where Length -eq 0
Upvotes: 0
Reputation: 23
Wow I had what I wanted mixed up! And I had to add the .count
to the getfiles()
method
$test | Where-Object {$_.PsISContainer} | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
Upvotes: 0
Reputation: 24555
I think you want this:
Get-ChildItem | Where-Object { (-not $_.PSIsContainer) -and ($_.Length -eq 0) }
If you have PowerShell 3.0 or later you can use this:
Get-ChildItem -File | Where-Object { $_.Length -eq 0 }
Of course you can add whatever other parameters for Get-ChildItem
that you want (-Recurse
, etc.).
Upvotes: 1