Given:
$path = c:\dir1\dir2\dir3\dir4\dir5
I want to search for var.txt
starting in the lowest child directory (dir5
).
If var.txt
is there, do something, if not, search the next level (in this case dir4
) for var.txt
and repeat.
Possibly relevant links:
How I could use test-path to check for var.txt
Ideas:
Somehow using Split-Path
, in a loop which iterates -Parent
, and using test-path
each iteration to check for var.txt
Solution:
Incorporating the solution below with actions, and breaking the loop if not found at the highest directory:
$file = ""
$path = ""
while($path -and !(Test-Path (Join-Path $path $file))){
if($path -eq ((Split-Path -Path $path -Qualifier)+"\")){
break
}
else {
$path = Split-Path $path -Parent
}
}
if($path -ne ((Split-Path -Path $path -Qualifier)+"\")){
#do something
}