问题描述
如何填充通过 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
是一个CLI程序,可在Windows和大多数Unix风格的OS上找到,用于绘制文件结构的直观表示.它不会生成目录数组.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!