问题描述
如何填充通过 tree
找到的文件数组?
How can an array of files, as found through tree
, be populated?
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>
这个例子没有子目录,但对于更复杂的目录,有子目录,希望捕获每个文件的完整路径和文件名.
This example has no sub-directories, but for more complex directories, with sub-directories, looking to capture, for each file, its full path and filename.
具体来说,然后搜索文件——但这个问题有点笼统.
Specifically, to then search the files -- but the question is of a somewhat general nature.
另见:
https://superuser.com/q/1270040/977796
推荐答案
tree
是 Windows 和大多数 Unix 风格操作系统上的 CLI 程序,用于绘制文件结构的可视化表示.它不会生成目录数组.
tree
is a CLI program found on windows and most Unix-style OSes for drawing a visual representation of a file structure. It does not generate an array of directories.
在 powershell 中,如果您想递归地获取目录结构的项目并将它们保存到变量(除非只返回一个项目,否则它将是一个数组)使用以下内容:
In powershell if you want to get the items of a directory structure recursively and save them to a variable (which would be an array except in the case where only a single item is returned) use the following:
$tree = Get-ChildItem -Recurse
这会将结果存储在变量 $tree
中.如果不想从当前位置开始,可以使用 -path
参数.例如
This will store the result in the variable $tree
.You can use the -path
parameter if you want to start not at the current location. E.g.
$tree = Get-ChildItem -Path "/home/dave" -Recurse
这篇关于如何将树的结果传递或管道到 Powershell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!