XIXIXIIXIIX
XIXIXIIXIIX

Reputation: 23

Finding Files in a directory equal to 0 Powershell

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

Answers (4)

Mathis Michel
Mathis Michel

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

Esperento57
Esperento57

Reputation: 17472

try this

Get-ChildItem "c:\temp" -File -Recurse | where Length -eq 0 

Upvotes: 0

XIXIXIIXIIX
XIXIXIIXIIX

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

Bill_Stewart
Bill_Stewart

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

Related Questions