How can I join the result of `ls` as a char separa

2019-08-12 04:18发布

I have a folder containing the following files:

  • File_1.txt
  • File_2.txt
  • AnotherFile.txt

I want to be able to achieve an output of: File_1.txt - File_2.txt

I have tried running:

ls .\file*.txt | %{$_.Name}

which returns what I want except on separate lines, how can I join the files returned by a given character (- in this example)?

Any help is appreciated.

2条回答
Juvenile、少年°
2楼-- · 2019-08-12 05:00

You can use the -join operator:

(ls file*) -join ' - '

If you need more control about the individual items being joined here, just amend the pipeline appropriately:

(ls file* | select -Expand Basename) -join ' - '
查看更多
祖国的老花朵
3楼-- · 2019-08-12 05:03

i'd do:

ls .\file*.txt | Foreach { $output = $output + $_ + ' - '}

查看更多
登录 后发表回答