1

This works to count *.jpg files.

PS C:\> @([System.IO.Directory]::EnumerateFiles('C:\Users\Public\Pictures', '*.jpg', 'AllDirectories')).Count
8

How can an -ErrorAction Continue be applied to this?

PS C:\> @([System.IO.Directory]::EnumerateFiles('C:\Users', '*.jpg', 'AllDirectories')).Count
An error occurred while enumerating through a collection: Access to the path 'C:\Users\Administrator' is denied..
At line:1 char:1
+ @([System.IO.Directory]::EnumerateFiles('C:\Users', '*.jpg', 'AllDire ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mklement0
  • 382,024
  • 64
  • 607
  • 775
lit
  • 14,456
  • 10
  • 65
  • 119
  • ...don't use a .NET call? `(Get-ChildItem -Path "C:\Users\Public\Pictures" -Recurse -File -Filter "*.pdf" -ErrorAction SilentlyContinue).Count` – gvee Jan 16 '19 at 14:55
  • Use `try{...} catch{...}` – Theo Jan 16 '19 at 15:08
  • 1
    @Theo That will catch the exception, but won't continue the operation. – Ansgar Wiechers Jan 16 '19 at 15:08
  • @AnsgarWiechers You're right there, but without having permissions on the path you will not get a correct count anyway if any, so this question is rather pointless I think.. – Theo Jan 16 '19 at 16:00
  • 2
    @Theo: There's still value in obtaining _all_ files that are _accessible_ while ignoring those that aren't. – mklement0 Jan 16 '19 at 16:12

3 Answers3

2

I don't think you can. Unless you want to implement directory traversal yourself you're probably stuck with something like this:

Get-ChildItem 'C:\Users' -Filter '*.jpg' -Recurse -Force -ErrorAction SilentlyContinue
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Ansgar Wiechers' helpful answer shows a workaround using Get-ChildItem, which is necessary when using the full, Windows-only .NET Framework (FullCLR), on which Windows PowerShell is built.

By contrast, .NET Core v2.1+ - on which PowerShell Core is built - does offer a solution:

@([System.IO.Directory]::EnumerateFiles(
  'C:\Users',
  '*.jpg', 
  [System.IO.EnumerationOptions] @{ 
    IgnoreInaccessible = $true
    RecurseSubDirectories = $true
  }
)).Count

Note that this is the equivalent of -ErrorAction Ignore, not Continue (or SilentlyContinue), in that inaccessible directories are quietly ignored, with no way to examine which of them were inaccessible afterwards.

The solution above is based on this System.IO.Directory.EnumerateFiles() overload, which offers a System.IO.EnumerationOptions parameter.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • So without jumping to PowerShell Core there's no other way around it to use the `[System.IO.Directory]::EnumerateFiles` as it's much faster than `Get-ChildItem`? I saw a C# method to get around it [here](https://stackoverflow.com/a/4986333/8262102). I wonder if it's possible for that to work in PS. – Ste Jul 23 '21 at 19:31
  • @Ste, I suspect that ad-hoc-compiling such C# code with `Add-Type` may be your only option. – mklement0 Jul 23 '21 at 19:53
  • 1
    I was thinking that was the only option. Thanks for the info. I don't know how to incorporate that but I think I might get away without the need atm. But if the need arises I'll ask a new Q on how to make that work. – Ste Jul 23 '21 at 20:35
0

The above answers work around the issue. They donnot appy the error action.

To realy catch the error action in the .net call, I'm using the $ErrorActionPreference variable in Windows PowerShell, as descirbed in https://devblogs.microsoft.com/scripting/handling-errors-the-powershell-way/:

# Store $ErrorActionPreference
$OldErrorActionPreference = $ErrorActionPreference
# Set $ErrorActionPreference for .net action
# see https://devblogs.microsoft.com/scripting/handling-errors-the-powershell-way/ for other values
$ErrorActionPreference = 'SilentlyContinue'
# .net call
@([System.IO.Directory]::EnumerateFiles('C:\Users\Public\Pictures', '*.jpg', 'AllDirectories')).Count
# restore origional $ErrorActionPreference
$ErrorActionPreference = $OldErrorActionPreference
CBee
  • 49
  • 2
  • This fails and doesn't gather the subsequent files after it encounters a folder it cannot access. – Ste Jul 23 '21 at 19:26