4 ms·
I still find it grating with how pipes are handled. E.g. git ls-files --exclude-standard -o | For-Each { Remove-Item $_ } Just.. _why_ do I need that For
by Jenk 5y ago
I still find it grating with how pipes are handled.
E.g.
git ls-files --exclude-standard -o | For-Each { Remove-Item $_ }
Just.. _why_ do I need that For-Each?!
(Deliberately not using aliases for clarity)
- tyingq 5y agoAs far as I can tell, you don't need it if you're piping the right stuff to Remove-Item C:\temp\foo> dir Directory: C:\temp\foo Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/10/2021 1:03 PM 0 thing1 -a---- 9/10/2021 1:03 PM 0 thing2 -a---- 9/10/2021 1:03 PM 0 thing3 C:\temp\foo> dir thing* | Remove-Item C:\temp\foo> dir C:\temp\foo>
- Jenk 5y agodir works because it's actually an alias of Get-ChildItem, which returns a list of objects (DirectoryInfo or FileInfo). My example was specifically a list of paths that are not objects but just strings.
- tyingq 5y agoOkay. How about "echo"? (Aliased to Write-Output, which I assume is strings) C:\temp> dir file3 Directory: C:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/10/2021 1:03 PM 0 file3 C:\temp> echo "file3" | Remove-Item C:\temp> dir file3 dir : Cannot find path 'C:\temp\file3' because it does not exist. Edit: I see it now. It works with one string, but if I Write-Output "file1`r`nfile2" | Remove-Item it no longer works because it takes the whole string as an object. I'd have to pipe an array of strings rather than cr-lf separated strings. This does work: C:\temp> $foo="file1`r`nfile2" C:\temp> $foo -split '\r?\n' | Remove-Item
- Jenk 5y agoYeah, another one I haven't worked out yet is opening mulitple files from shell. So far this is what I can come up with: @(Get-ChildItem *.ts | foreach { $_.Name } | Join-String -Separator ' ') | foreach { nvim -- $_ } But even that abomnination doesn't quite work.
- deleted 5y ago[deleted]
- jiggawatts 5y agoI suspect that this is sufficient for your needs: nvim -- (( dir *.ts ).Name -join ' ') I don't have nvim, but the equivalent worked for me with "code" (VS Code).
- Jenk 5y agoThat does indeed work but it's quite different from piping. But thanks that has increased my productivity! I use fzf a lot so I'll be adapting it for that too :)