从带有通配符的路径获取实际路径

use*_*839 3 powershell if-statement

Test-Pathif语句中使用时,我希望获得if语句成功的路径.

例如,这些文件存在于C中:

C:\Test6_1_15.txt
C:\Test6_2_15.txt
C:\Test6_3_15.txt
C:\Test6_4_15.txt
Run Code Online (Sandbox Code Playgroud)

我在"then"分支怎么办?

$Path = "C:\Test6_*_15.txt"
if (Test-Path $Path)
{
   # if test passes because there are 4 files that fit the test, but I want to be
   # able to output the file that made the if statement succeed. 
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 5

听起来像你想要的Resolve-Path:

if(($Paths = @(Resolve-Path "C:\Test6_*_15.txt"))){
    foreach($file in $Paths){
        # do stuff
    }
} else {
    # Resolve-Path was unable to resolve "C:\Test6_*_15.txt" to anything
}
Run Code Online (Sandbox Code Playgroud)