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:
- Get *.csproj files as specified above
- Get folders containing them
- Combine folder paths with "bin" subfolder name
- Test "bin" folders exist
- 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:
- To be able to use relative path from "Tools" folder which is current directory for executed script
- Not to change current directory during script execution,
- 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 !!!