使用 Test-path 检查多个路径 [英] Check multiple path with Test-path
问题描述
在我的函数中,我有 3 个没有强制性和位置参数 Folder1、Folder2 和 Folder3
,在运行我的函数之前,我想检查每个选定的参数是否存在文件夹.它必须是动态的,因为用户可以随机指定一两个或所有树文件夹.提前致谢.
In my function i have 3 no mandatory and no positional parameters Folder1, Folder2 and Folder3
, before running my function i would like to check for each selected parameter if folder exist. It must be something dynamic because user can specify randomly one or two or all tree folders.
Thanks in advance.
推荐答案
您可以将数组传递给 Test-Path
You can pass an array to Test-Path
Test-Path c:, d:, e:
True
True
True
编辑
所以我从你的评论中相信你有这样的事情:
So I believe from your comment that you have something like this:
$mypaths = "c:","d:",$null
Test-Path $mypaths
出现以下错误:
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
那你可以试试这个:
$mypaths = "c:","d:",$null
$mypaths | Foreach { if ($_){Test-Path $_}}
这导致只有两个有效路径.
Which results in only two valid paths.
所以你可以考虑检查返回值:
So you could consider checking the returned values:
$valid = $mypaths | Foreach { if ($_){Test-Path $_}}
write-host "Variable `$mypaths contains $($mypaths.count) paths, and $($valid.count) were found valid"
输出:
Variable $mypaths contains 3 paths, and 2 were found valid
这篇关于使用 Test-path 检查多个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!