【问题标题】:How to pass or pipe the results of tree to Powershell?如何将树的结果传递或管道传递给 Powershell?
【发布时间】:2021-05-24 07:18:15
【问题描述】:

如何填充通过tree 找到的文件数组?

posh> 
posh>  tree ./  | Get-Item

    Directory: /home/nicholas/powershell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2/21/2021  5:42 PM                regex
Get-Item: Cannot find path '/home/nicholas/powershell/regex/├── a.log' because it does not exist.
..
Get-Item: Cannot bind argument to parameter 'Path' because it is an empty string.
Get-Item: Cannot find path '/home/nicholas/powershell/regex/0 directories, 16 files' because it does not exist.

posh> 

此示例没有子目录,但对于更复杂的目录,带有子目录,希望为每个文件捕获其完整路径和文件名。

具体来说,然后是 search 文件 -- 但这个问题有点笼统。

另见:

Use PowerShell to generate a list of files and directories

https://superuser.com/q/1270040/977796

【问题讨论】:

  • 我对您的要求感到困惑?你能改述一下你想做什么吗?

标签: powershell loops foreach tree get-childitem


【解决方案1】:

tree 是一个 CLI 程序,可以在 Windows 和大多数 Unix 风格的操作系统上找到,用于绘制文件结构的可视化表示。它不会生成目录数组。

在 powershell 中,如果您想递归地获取目录结构的项目并将它们保存到一个变量(这将是一个数组,除非只返回一个项目),请使用以下内容:

$tree = Get-ChildItem -Recurse

这会将结果存储在变量$tree 中。 如果您不想从当前位置开始,可以使用-path 参数。例如

$tree = Get-ChildItem -Path "/home/dave" -Recurse

【讨论】:

  • 很好,但您能详细说明一下带有tree 实用程序的类Unix 平台吗?例如,macOS 11 和 Ubuntu 18.04 都没有配备。由于 Windows 上 tree.com 的默认行为只是打印 目录,因此类似的 Get-ChildItem 调用需要 -Directory 开关。
  • 我记得我必须安装 tree @mklement0 (for ubuntu)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
相关资源
最近更新 更多