开发者

Powershell: Why does (gci c:\ddd).count on an Empty folder not return 0

开发者 https://www.devze.com 2023-01-28 22:23 出处:网络
Why does (gci c:\\ddd).count on an Empty folder not return 0 but \"nothing\" I just get an Error \"You cannot call a method on a null-valued expression.\" When my count-condition does 开发者_如何学Py

Why does (gci c:\ddd).count on an Empty folder not return 0 but "nothing"

I just get an Error "You cannot call a method on a null-valued expression." When my count-condition does 开发者_如何学Pythonnot match.

What do i need to "get" the zero to prevent the exception?


Use the operator @() to make sure that the result is an array, including empty or containing a single item:

@(gci c:\ddd).count

Commands may return: 1) a collection; 2) a single object; 3) null. Your case is 3. Calling .Count on null (case 3) or on an object that does not have a property Count (case 2) gets nothing or may fail, for example, with strict mode enabled Set-StrictMode -Version 2.

@(...) is always an array and Count works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号