views:

76

answers:

1

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?

+3  A: 

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 })"
dahlbyk
+1. Even though I'd use `-join` or `[string]::join` which is probably less side-effect-prone. with `$OFS` I tend to overlook it when reading code.
Joey
Works like a charm.
fatcat1111
@Johannes - Agreed, just trying to give some options.
dahlbyk