Search code examples
powershell7zip

"-split"-ing the output from "-stream"?


I have a piece of PS code which takes the 7-Zip extraction output and filters it down so only percentage "%" progress update lines get printed.

Some of the lines that are printed by the console have other things on them too after the percentage count (like the file that's currently extracting).

These are always after . (space, period, another space). Is there any way to cut down the strings that are printed to remove anything after the .? I can't seem to use a -split because it has to be at the start of the pipeline!

Code:

"Starting decompression of [$filePath]..."
& $7ZipPath "x" $filePath "-o$extractionPath" "-aos" "-bsp1" | out-string -stream | select-string %

Example Output:

  0%
  1%
  4%
  7%
 11%
 14%
 17%
 20%
 22%
 25%
 29%
 34%
 39%
 44%
 46%
 48%
 50%
 52%
 54%
 56%
 58%
 60%
 61%
 63%
 65%
 67%
 69%
 71%
 73%
 75%
 77%
 78% . 4x4 Evo (USA).bin
 79% . 4x4 Evo (USA).bin
 81% . 4x4 Evo (USA).bin
 82% . 4x4 Evo (USA).bin
 83% . 4x4 Evo (USA).bin
 84% . 4x4 Evo (USA).bin
 86% . 4x4 Evo (USA).bin
 87% . 4x4 Evo (USA).bin
 88% . 4x4 Evo (USA).bin
 89% . 4x4 Evo (USA).bin
 91% . 4x4 Evo (USA).bin
 92% . 4x4 Evo (USA).bin
 93% . 4x4 Evo (USA).bin
 94% . 4x4 Evo (USA).bin
 95% . 4x4 Evo (USA).bin
 97% . 4x4 Evo (USA).bin
 98% . 4x4 Evo (USA).bin
 99% . 4x4 Evo (USA).bin
100% . 4x4 Evo (USA).bin

Solution

  • I don't like solutions like that, where you pipe a lot. Maybe you should consider to use Expand-Archive

    But this line will work:

    "100% . 4x4 Evo (USA).bin" | Select-String -Pattern "\d{1,3}%" -AllMatches | ForEach-Object { $_.Matches.Value }