3

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

Recursion, maybe?

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
}
meue
  • 148
  • 8
  • Have you tried something, and if so, can you show us? SO is best used as a resource for resolving specific issues when something doesn't work. – Kai Aug 11 '17 at 19:39
  • Hi Kai, aplogies. The purpose was to find the root directory of a project. Through a script, each file in all the sub-directories of the local root directory would be copied over to a network drive, keeping the folder structure preserved. I figured I would incorporate txt file that the script would search for to get the paste destination. However, because a project folder lives in a client folder, and a client can have multiple projects, I would need to parse a file's path from right to left. I started using PowerShell today and only had an idea. My ideas were actuated in the solution. – meue Aug 11 '17 at 21:54

2 Answers2

5

Try this (recursive method) :

function GetPathIfFileExist($pathtosearch, $filename)
{
   if($pathtosearch -eq "")
   {
       "file not founded"
   }
   elseif (Test-Path "$pathtosearch\$filename" )
   {
       $pathtosearch
   }
   else
   {
      GetPathIfFileExist (Split-Path $pathtosearch) $filename
   }
}

GetPathIfFileExist "c:\dir1\dir2\dir3\dir4\dir5" "var.txt"
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • 1
    Thank you for this alternate solution - is there a benefit of using this recursive method over the while split-path method? – meue Aug 11 '17 at 21:57
  • 1
    I dont think there is a really difference, except to readability. Do your choice :) – Esperento57 Aug 12 '17 at 07:08
4
$path = 'c:\dir1\dir2\dir3\dir4\dir5'
while($path -and !(Test-Path (Join-Path $path 'var.txt'))){
    $path = Split-Path $path -Parent
}
Write-Output (Join-Path $path 'var.txt')

Assumes that var.txt exists in one of those dirs, and that you know the path.

E.V.I.L.
  • 2,120
  • 13
  • 14
  • 1
    Apologies sir - I had upvoted you, but because I have less than 15 reputation I do not think it shows publicly. The question also garnered negative points, insult to injury. This solution worked as intended. I inserted an `if` and `else` statement within the `while` loop to do given actions. – meue Aug 11 '17 at 21:13