Dravidian
Dravidian

Reputation: 339

Count the number of files under a directory (including files under subdirectories)

I am trying to find the total number of files under a directory which is present on my company's shared drive. But I do not have the permission to access some of the folders (Strictly restricted) inside several subdirectories. I am using Windows PowerShell but getting many errors saying, PERMISSION DENIED along with the path of the folder which it inaccessible. Below is the command I used,

Get-ChildItem -Recurse | Measure-Object | %{$_.Count}

My question is, Is there a way to find the total number of files in all the directories and subdirectories, ignoring those restricted files which are causing the errors?

Upvotes: 1

Views: 203

Answers (3)

sodawillow
sodawillow

Reputation: 13176

Let me try:

Get-Childitem "C:\Users" -File -Recurse -ErrorAction SilentlyContinue |
    Measure-Object |
    Select-Object -ExpandProperty Count

Upvotes: 0

Esperento57
Esperento57

Reputation: 17462

just do it

  (gci -Path C:\temp -File -Recurse -ErrorAction SilentlyContinue).Count

Upvotes: 1

restless1987
restless1987

Reputation: 1598

Powershell 3.0 and higher:

(Get-Childitem -Recurse -path x:\whatever -erroraction SilentlyContinue | where {-not ($_.PSIsContainer)}).count

You might still see errors, but you'll get an answer

Upvotes: 0

Related Questions