【问题标题】:Passing directory contents as a single line to an executable in Powershell将目录内容作为单行传递给 Powershell 中的可执行文件
【发布时间】:2010-12-06 18:40:36
【问题描述】:

我有一个二进制可执行文件,它以文件路径列表作为参数,例如,

C:\Tool.exe C:\Files\File1.txt C:\Files\File2.txt

我想从 Powershell 调用这个工具。问题是,我怎样才能在一行上得到 get-childitem 的输出?

如果我跑步:

ls C:\Files\*.txt |选择全名

我每行有一条路径。如何连接结果?

【问题讨论】:

    标签: powershell cat get-childitem


    【解决方案1】:

    在 PowerShell 2.0 中,您可以使用 -join 运算符:

    (ls C:\Files\*.txt | %{ $_.FullName }) -join ' '
    

    在 PowerShell 1.0 中可以设置$OFS,当作为字符串使用时,用于组合一系列项:

    $ofs = ' '
    "$(ls C:\Files\*.txt | %{ $_.FullName })"
    

    【讨论】:

    • +1。即使我会使用-join[string]::join,这可能不太容易产生副作用。与$OFS 一起阅读代码时我倾向于忽略它。
    • @Johannes - 同意,只是想提供一些选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多