使用单个命令列出2个文件夹

时间:2018-08-07 10:03:33

标签: windows powershell command-line powershell-v2.0

假设我有两个文件夹A和B

Folder A contains files like "food.txt", "drinks.txt", ...
Folder B contains files like "beverage.txt", "entre.txt",...

有没有一种方法可以使用dir(gci)或任何命令来获取类似

的输出
"beverage.txt", "drinks.txt", "entre.txt", "food.txt",..

2 个答案:

答案 0 :(得分:3)

只需使用:

Get-ChildItem -File -Recurse | Select-Object -ExpandProperty Name

-File将仅列出文件 -Recurse还将在给定文件夹下获得所有文件夹

Select-Object -Expandproperty Name仅会给您文件名


如果文件位于完全不同的位置,则可以给GCI提供多个路径:

Get-ChildItem -File -Path "PathToFolderA","PathToFolderB" | Select-Object -ExpandProperty Name

答案 1 :(得分:1)

PSv3之前检查文件/目录的方法是检查PSIsContainer中的Where-Object属性

因此此行将返回:

PS A:\> Get-ChildItem -Path "Folder A","Folder B" |Where-object {!$_.PSIsContainer}| Select-Object -ExpandProperty Name
drinks.txt
food.txt
beverage.txt
entre.txt

用双引号和,分隔问题字面量

(Get-ChildItem -Path "Folder A","Folder B" |Where-object {!$_.PSIsContainer}|ForEach-Object { '"{0}"' -f $_.Name}) -join ', '
"drinks.txt", "food.txt", "beverage.txt", "entre.txt"

要对输出进行排序,只需插入|Sort-Object Name

(Get-ChildItem -Path "Folder A","Folder B" |sort Name|Where-object {!$_.PSIsContainer}|ForEach-Object { '"{0}"' -f $_.Name}) -join ', '
"beverage.txt", "drinks.txt", "entre.txt", "food.txt"