Passing directory contents as a single line to an executable in Powershell
-
21-09-2019 - |
Question
I have a binary executable that takes a list of file paths as arguments, e.g.,
C:\Tool.exe C:\Files\File1.txt C:\Files\File2.txt
I would like to call this tool from Powershell. The question is, how can I get the output of get-childitem all on one line?
If I run:
ls C:\Files\*.txt | select FullName
I get one path per line. How can I concatenate the results?
Solution
In PowerShell 2.0 you can use the -join operator:
(ls C:\Files\*.txt | %{ $_.FullName }) -join ' '
In PowerShell 1.0 you can set $OFS
, which is used to combine a sequence of items when they are used as a string:
$ofs = ' '
"$(ls C:\Files\*.txt | %{ $_.FullName })"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow