Get actual path from path using template
When used Test-Path
in a statement if
, I am looking to get the path at which the if statement is executed.
For example, these files exist in C:
C:\Test6_1_15.txt
C:\Test6_2_15.txt
C:\Test6_3_15.txt
C:\Test6_4_15.txt
what should I do in the "then" branch?
$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. }
source to share
It sounds like you want 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
}
source to share
You won't get it with Test-Path
. Test-Path
returns boolean value (s) representing the presence of the traversed path (s). Looking at the description from TechNet
It returns TRUE ($ true) if all elements exist and FALSE ($ false) if they are absent
If you only want the filenames that match, then use Get-Item
as it supports standard wildcards. You can get information from objects System.IO.FileInfo
returned Get-Item
.
source to share