8

So, I've got a set of directories 00-99 in a folder. Each of those directories has 100 subdirectories, 00-99. Each of those subdirectories has thousands of images.

What I'm attempting to do is basically get a progress report while it's computing the average file size, but I can't get that to work. Here's my current query:

get-childitem <MyPath> -recurse -filter *.jpeg | Where-Object { Write-Progress "Examining File $($_.Fullname)" true } | measure-object -Property length -Average

This shows me a bar that updates as each of the files gets processed, but at the end I get back no average file size data. Clearly, I'm doing something wrong, because I figure trying to hack the Where-Object to print a progress statement is probably a bad idea(tm).

Since there are millions and millions of images, this query obviously takes a VERY LONG time to work. get-childitem is pretty much going to be the bulk of query time, if I understand things correctly. Any pointers to get what I want? AKA, my result would ideally be:

Starting...
Examining File: \00\00\Sample.jpeg
Examining File: \00\00\Sample2.jpeg
Examining File: \00\00\Sample3.jpeg
Examining File: \00\00\Sample4.jpeg
...
Examining File: \99\99\Sample9999.jpg
Average File Size: 12345678.244567

Edit: I can do the simple option of:

get-childitem <MyPath> -recurse -filter *.jpeg | measure-object -Property length -Average

And then just walk away from my workstation for a day and half or something, but that seems a bit inefficient =/

Tejs
  • 40,736
  • 10
  • 68
  • 86
  • 1
    If anyone else is wondering why the example in OP doesn't work, its because the script block passed to the `Where-Object` doesn't return a boolean value. It could if it was changed to this: ```...Where-Object { Write-Progress "Examining File $($_.Fullname)"; $true }...``` – jag Jan 30 '20 at 15:36

2 Answers2

13

Something like this?

get-childitem -recurse -filter *.exe | 
      %{Write-Host Examining file: $_.fullname; $_} | 
      measure-object -Property length -Average
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Im curious, as Im not as familiar with powershell. The % with expression doesnt change the value of the item being piped or something? Can you explain the expression so I can get greater understanding? – Tejs May 02 '11 at 21:21
  • 2
    Basically receive the object, and write it `Write-Host Examining file: $_.fullname;` and pass the object downstream `$_` – manojlds May 02 '11 at 21:25
  • 3
    And `%` is same as `ForEach-Object` – manojlds May 02 '11 at 21:26
  • 4
    `%` is overloaded, actually. It's an alias for `ForEach-Object` and it's also the modulo (or modulus) operator. – Bacon Bits May 03 '11 at 01:07
  • 1
    Very nice display of the difference between the output to the host, and the objects passing through the pipeline. – Daniel Richnak May 03 '11 at 05:02
5

A little more detailed progress:

$images = get-childitem  -recurse -filter *.jpeg

$images | % -begin { $i=0 } `
-process {  write-progress -activity "Computing average..." -status "Examining File: $image.fullpath ($i of $($images.count))" -percentcomplete ($i/$images.count*100); $i+=1 } `
-end { write-output "Average file size is: $($images | measure-object -Property length -Average)" }
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
  • 1
    I was curious to try your solution but when I paste your script, powershell shows me ">>". Is there anything missing? – Nicola Cossu May 02 '11 at 22:12
  • 2
    Ops...I think it's due to missing closing double quotes in the `end` block. I've edited the answer. It should work now. Thanks. +1 for giving it a try and feedback :) – Emiliano Poggi May 02 '11 at 22:27
  • 2
    Now the script runs but write-output returns "Average file size is: Microsoft.PowerShell.Commands.GenericMeasureInfo". If I change last part of code in this way: write-output "Average file size is:" $($images | measure-object -property length -average).Average it seems to work. Still +1. Your post are always instructives. Bye. – Nicola Cossu May 02 '11 at 23:04
  • 1
    Thanks for testing it. It's true, measure-object takes `inputobject` parameter and I was passing to it the array of objects. Using the pipeline all is resolved. Thanks again. I'm glad you like my answers. +1 for cheered up my answer :D – Emiliano Poggi May 02 '11 at 23:12