使用 PowerShell 生成文件和目录列表 [英] Use PowerShell to generate a list of files and directories

查看:152
本文介绍了使用 PowerShell 生成文件和目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 PowerShell 脚本来创建多个目录并将一堆文件复制到一起以编译"一些技术文档.我想生成文件和目录的清单作为自述文件的一部分,我希望 PowerShell 执行此操作,因为我已经在 PowerShell 中进行编译".

I'm writing a PowerShell script to make several directories and copy a bunch of files together to "compile" some technical documentation. I'd like to generate a manifest of the files and directories as part of the readme file, and I'd like PowerShell to do this, since I'm already working in PowerShell to do the "compiling".

我已经做了一些搜索,似乎我需要使用 cmdletGet-ChildItem",但是它给了我太多的数据,我不清楚如何格式化和修剪我的不想得到我想要的结果.

I've done some searching already, and it seems that I need to use the cmdlet "Get-ChildItem", but it's giving me too much data, and I'm not clear on how to format and prune out what I don't want to get my desired results.

我想要类似这样的输出:

I would like an output similar to this:

Directory
     file
     file
     file
Directory
     file
     file
     file
     Subdirectory
          file
          file
          file

或者可能是这样的:

+---FinGen
|   \---doc
+---testVBFilter
|   \---html
\---winzip

换句话说,树结构的某种基本的可视化 ASCII 表示,包括目录和文件名,没有别的.我见过可以执行此操作的程序,但我不确定 PowerShell 是否可以执行此操作.

In other words, some kind of basic visual ASCII representation of the tree structure with the directory and file names and nothing else. I have seen programs that do this, but I am not sure if PowerShell can do this.

PowerShell 能做到这一点吗?如果是这样,Get-ChildItem 是正确的 cmdlet 吗?

Can PowerShell do this? If so, would Get-ChildItem be the right cmdlet?

推荐答案

在您的特定情况下,您需要的是 Tree/f.您有一条评论询问如何去掉前面谈论卷、序列号和驱动器号的部分.这可以在将输出发送到文件之前过滤输出.

In your particular case what you want is Tree /f. You have a comment asking how to strip out the part at the front talking about the volume, serial number, and drive letter. That is possible filtering the output before you send it to file.

$Path = "C:\temp"
Tree $Path /F | Select-Object -Skip 2 | Set-Content C:\temp\output.tkt

上面例子中

Tree 的输出是一个我们可以操作的System.Array.Select-Object -Skip 2 将删除包含该数据的前两行.此外,如果 Keith Hill 在附近,他还会推荐包含 cmdlet Show-Tree 的 PowerShell 社区扩展 (PSCX).如果您好奇,请从此处下载.那里有很多强大的东西.

Tree's output in the above example is a System.Array which we can manipulate. Select-Object -Skip 2 will remove the first 2 lines containing that data. Also, If Keith Hill was around he would also recommend the PowerShell Community Extensions(PSCX) that contain the cmdlet Show-Tree. Download from here if you are curious. Lots of powerful stuff there.

这篇关于使用 PowerShell 生成文件和目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆