简体   繁体   English

如何在Powershell中以逗号分隔的字符串形式获取目录中的所有文件名?

[英]How can I get all the file names in a directory as a comma separated string in Powershell?

I want to get all the names in a directory as a comma separated string so that I can pass them as a parameter to a cmdlet.我想将目录中的所有名称作为逗号分隔的字符串获取,以便我可以将它们作为参数传递给 cmdlet。

How can I get all the file names in a directory as a comma separated string in Powershell?如何在Powershell中以逗号分隔的字符串形式获取目录中的所有文件名?

一种方法是:

 (dir  | % { $_.basename }) -join ','

This should work:这应该有效:

(ls C:\PATH\TO\FOLDER | select -expandproperty name) -join ','

If there are subfolders in there that you want to avoid:如果那里有您想要避免的子文件夹:

(ls C:\PATH\TO\FOLDER | ?{$_.PSIsContainer -eq $false} | select -expandproperty name) -join ','

Quickest/shortest way:最快/最短的方式:

(ls).name -join ','

For specific folder-path:对于特定的文件夹路径:

(ls path\to\folder).name -join ','

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM