Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

powershell - Get-ChildItem with multiple paths - Error if directory is missing

I am using Get-ChildItem to collect files in multiple paths.

For example

Get-ChildItem -Path c:Test1, c:Test2 -Filter *.xmd -Recurse -File

If one of the directories is missing I get errors like these

Get-ChildItem : Access to the path 'C:windowsemp" is denied.

First, I do not understand why this directory is searched and second what is the way to avoid this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm guessing that one of those paths does not exists. Get-ChildItem has a few counter-intuitive behaviours. It may be a mix of interpreting the input, legacy functionality and maybe a bug or two.

If you don't use -Recurse, you'll get Cannot find path ... error as expected. It will also work properly if you add backslashes to paths:

Get-ChildItem -Path c:Test1, c:Test2 -Filter *.xmd -Recurse -File

or use -LiteralPath (-Path accepts wildcards):

Get-ChildItem -LiteralPath c:Test1, c:Test2 -Filter *.xmd -Recurse -File

Add -ErrorAction Continue or -ErrorAction SilentlyContinue if you don't want execution to stop at missing path error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...