0

I'm new to PowerShell. I've spend almost two days experimenting on finding some folders. Whatever I do it doesn't work. I have a following structure in file system.

My-GitRepo
|-Src
  |-MyCSharpProject_A
    |-bin
    |-MyCSharpProject_A.csproj
  |-MyCSharpProject_B
    |-bin
    |-MyCSharpProject_B.csproj
  |-MyCSharpProject_C
    |-bin
    |-MyCSharpProject_C.csproj
|-Tools
  |-MyPowerShellScript.ps1

I'm currently working on this "MyPowerShellScript.ps1" file. What I'm trying to do is to get *.csproj files from "Src" folder and it works Get-ChildItem -Path "..\Src\*\*.csproj". But the goal is a bit more complicated. I tried to use Get-ChildItem with pipeline to:

  1. Get *.csproj files as specified above
  2. Get folders containing them
  3. Combine folder paths with "bin" subfolder name
  4. Test "bin" folders exist
  5. And put existing "bin" folder paths into local array variable.

As a result I need to dynamically create an array that I currently have hard-coded in my script.

$folders = (
    "..\Src\MyCSharpProject_A\bin",
    "..\Src\MyCSharpProject_B\bin",
    "..\Src\MyCSharpProject_C\bin",
);

Get-ChildItem -Path "..\Src\*\bin" is not what I'm looking for. I'm trying to find "bin" folders that exists next to csproj files.

If I could write the same in C#, it would be one of the following:

string[] qryA = new DirectoryInfo(Environment.CurrentDirectory)
    .Parent
    .GetDirectories(@"Src")
    .Single()
    .GetDirectories()
    .SelectMany(d => d.GetFiles(@"*.csproj"))
    .Select(f => f.Directory.FullName)
    .Select(d => Path.Combine(d, "obj"))
    .Where(d => Directory.Exists(d))
    .ToArray();
string[] qryB = new DirectoryInfo(Environment.CurrentDirectory)
    .Parent
    .GetDirectories(@"Src")
    .Single()
    .GetDirectories()
    .SelectMany(d => d.GetFiles(@"*.csproj"))
    .Select(f => f.Directory.GetDirectories("obj").SingleOrDefault())
    .Where(d => d?.Exists == true)
    .Select(d => d.FullName)
    .ToArray();

What is important:

  1. To be able to use relative path from "Tools" folder which is current directory for executed script
  2. Not to change current directory during script execution,
  3. To have single pipeline (more functional style) which I can convert into function later and share it among many scripts.

UPDATE: Now I have something that works but it still does not have the form of the pipeline:

$csProjs = Get-ChildItem -Path "..\Src\*\*.csproj" -File;
$folders = @();

foreach ($csProj in $csProjs)
{
    $prjFolder = Split-Path $csProj;
    $objPath = Join-Path $prjFolder "obj";

    if (Test-Path $objPath -PathType Container)
    {
        $folders += $objPath;
    }
}

Is it possible to convert it to a pipeline?

Please HELP !!!

1 Answer 1

0

Well, if I got your question right, here is something that would do it.

  1. Get *.csproj file - Accomplished with Get-ChildItem with -Filter parameter.
  2. Get folders containing them - This can be done with Split-Path
  3. Combine folders path with "bin" subfolder name - Simple string concatenation
  4. Test "bin" folder exist - Test-Path
  5. And put "bin" folder paths into local array variable. - Since your example showed a relative path, I used Set-Location along with Resolve-Path with -Relative switch parameter

Code

$BasePath = 'PathToSearch'
# Set-Location - This is so we get relative paths
Set-location $BasePath

$CsProjects = Get-ChildItem -Path $BasePath -Filter '*.csproj' -File -recurse

$OutPaths = Foreach ($CSP in $CsProjects) {
    $Binpath = "$(Split-Path $CSP.FullName)\bin"
    if (Test-Path $Binpath) {
        Resolve-Path  $Binpath -Relative
    }
}

Output based on my search

.\ConsoleHook\bin
.\ConsoleHook.Rx\bin
.\FormsExample\bin
Sign up to request clarification or add additional context in comments.

1 Comment

I've just added little update to my question to clarify what I'm looking for. It's based on Split-Path, Join-Path and test-Path. Is it possible? The result must contain full paths of "bin" folders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.