powershell get-childitems without newline

Viewed 36

I like to get all *.csproj files within my folders (recursive) and later "tar" them into a tarball. I like to follow this approach but on PowerShell instead of bash.

What I came already up with is this:

gci -r -fi *.csproj | Resolve-Path -Relative | tar -cvf ball.tar --null -T -

The main problem now is, that I get the error

Couldn't find file: No such file or directory

I assume this is, because in contrast to bash, PowerShell outputs the files using a NewLine at the end of each found entry.

How can I remove the NewLine or avoid that it's added in the first place?

1 Answers

The problem isn't with PowerShell, it's the use of tar's --null option, which expects the file paths provided via stdin to be null-byte- rather than newline-separated (the latter being what PowerShell uses).

Simply omitting --null should do:

gci -r -fi *.csproj | Resolve-Path -Relative | tar -cvf ball.tar -T -

Alternatively - assuming you don't run into command-line length limitations with a large count of matching files - you can pass the paths directly, relying on PowerShell's ability to automatically pass multiple output objects from an expression as individual arguments when calling external programs:

tar -cvf ball.tar (gci -r -fi *.csproj | Resolve-Path -Relative)
Related