【问题标题】:Powershell - How to finding all files of specified type and the folders next to themPowershell - 如何查找指定类型的所有文件及其旁边的文件夹
【发布时间】:2019-12-09 14:59:04
【问题描述】:

我是 PowerShell 新手。我花了将近两天的时间尝试寻找一些文件夹。不管我做什么都行不通。我在文件系统中有以下结构。

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

我目前正在处理这个“MyPowerShellScript.ps1”文件。我想做的是从“Src”文件夹中获取 *.csproj 文件,它可以工作Get-ChildItem -Path "..\Src\*\*.csproj"。但目标有点复杂。我尝试将 Get-ChildItem 与管道一起使用:

  1. 按上述方式获取 *.csproj 文件
  2. 获取包含它们的文件夹
  3. 将文件夹路径与“bin”子文件夹名称结合起来
  4. 测试“bin”文件夹是否存在
  5. 并将现有的“bin”文件夹路径放入本地数组变量中。

因此,我需要动态创建一个我目前在脚本中硬编码的数组。

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

Get-ChildItem -Path "..\Src\*\bin" 不是我想要的。我正在尝试查找存在于 csproj 文件旁边的“bin”文件夹。

如果我可以用 C# 编写相同的代码,它将是以下之一:

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();

什么是重要的:

  1. 能够使用“工具”文件夹中的相对路径,该文件夹是执行脚本的当前目录
  2. 在脚本执行期间不要改变当前目录,
  3. 拥有单一管道(更多功能样式),我可以稍后将其转换为函数并在多个脚本之间共享。

更新:现在我有了一些可以工作的东西,但它仍然没有管道的形式:

$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;
    }
}

可以转成管道吗?

请帮忙!!!

【问题讨论】:

    标签: powershell pipeline get-childitem


    【解决方案1】:

    好吧,如果我的问题是正确的,那么可以这样做。

    1. 获取 *.csproj 文件 - 使用 Get-ChildItem-Filter 参数完成。
    2. 获取包含它们的文件夹 - 这可以通过 Split-Path 完成
    3. 将文件夹路径与“bin”子文件夹名称组合 - 简单字符串连接
    4. 测试“bin”文件夹是否存在 - Test-Path
    5. 并将“bin”文件夹路径放入本地数组变量中。 - 由于您的示例显示了相对路径,因此我使用 Set-LocationResolve-Path-Relative 开关参数

    代码

    $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
        }
    }
    

    根据我的搜索输出

    .\ConsoleHook\bin
    .\ConsoleHook.Rx\bin
    .\FormsExample\bin
    

    【讨论】:

    • 我刚刚对我的问题添加了一些更新,以澄清我在寻找什么。它基于 Split-Path、Join-Path 和 test-Path。是否可以?结果必须包含“bin”文件夹的完整路径。
    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多