create a list of the attributes of files with cmdlet get-member

1)How to create a list of attributes of files with cmdlet get-member and then sort it by last write time?

2)Find total size of files with different extension(for examp total size for all *.html files)

I think the solution for the first task(second task is ok) should be like this(however it doesn't work)

 $a=get-childitem . -filter *.html
 $n=$a.Length
 do{
 $isnotsorted=0
 for($i=0;$i -lt ($n-1); $i++) {
  if ((get-member $a[$i]).LastWriteTime -lt (get-member $a[$i]).LastWRiteTime){
      $a[$i],$a[$i+1]=`
      $a[$i+1],$a[$i]
      $isnotsorted=$i+1
  }
 }
 $n=$isnotsorted
 }
 until ($n -eq 0)
 $a

Upvotes: 0

Views: 6250

Answers (2)

Andy Arismendi
Andy Arismendi

Reputation: 52639

  1. You don't need to use Get-Member to do this. You can use Sort-Object and Select-Object:

    dir C:\ -Force | ? {!$_.PsIsContainer} | Sort LastWriteTime | Select FullName, Attributes
    
  2. You can use Group-Object and Measure-Object to do this.

    ((dir D:\Software -Force -Filter *.html | Group Extension).Group | Measure-Object -Sum Length).Sum / 1MB
    

I'm not sure why you don't want to use Sort-Object -Property LastWriteTime but here is how you would fix your bubble sort code. Remember Get-Member is not the right cmdlet to use to access a properties value.

$a = get-childitem -filter *.html
$n = $a.Length

do {
    $isnotsorted = 0
    for($i = 0; $i -lt ($n-1); $i++) {
        if ( ($a[$i]).LastWriteTime -lt ($a[$i + 1]).LastWRiteTime ) {
            $a[$i] , $a[$i+1] = $a[$i+1] , $a[$i]
            $isnotsorted = $i + 1
        }
    }   
    $n = $isnotsorted
} until ($n -eq 0)
$a

Another thing to note here is that the performance of this algorithm is much worse than just using Sort-Object. My music folder has 1355 files and the above finishes in 83 seconds. Using Sort-Object finishes in 1.7 seconds.

Measure-Command {
    get-childitem D:\shares\Music -rec -filter *.m4a | Sort-Object LastWriteTime
}

Upvotes: 5

Lance U. Matthews
Lance U. Matthews

Reputation: 16612

You don't need Get-Member to display the attributes of files. Just use Get-ChildItem to get the contents of a directory and then pipe them to Sort-Object:

Get-ChildItem -Path $path | Sort-Object -Property 'LastWriteTime'

You can add the -Recurse parameter to Get-ChildItem to list child directories, and add -Force to list files with the Hidden attribute. You can pipe all of this to a Format-* cmdlet if you want to display properties other than those displayed by the standard formatting for files and directories:

Get-ChildItem -Path $path `
    | Sort-Object -Property 'LastWriteTime' `
    | Format-Table -Property @('Attributes', 'FullName', 'CreationTime')

Get-Member can be used to determine which properties exist on a file or directory object.

You can use Measure-Object with the -Sum switch to add up the Length property of a collection of files:

$htmlFiles = Get-ChildItem -Path $path -Filter '*.html';
$measurement = $htmlFiles | Measure-Object -Property 'Length' -Sum;
$totalHtmlSize = $measurement.Sum;

To generate a table of the total size of each file type you can do something like this:

Get-ChildItem -Path $path `
    | Where-Object { $_ -is [IO.FileInfo]; } `
    | Group-Object -Property 'Extension' `
    | Select-Object `
        @{ Name = 'Extension'; Expression = 'Name' }, `
        @{ Name = 'TotalSize'; Expression = { `
            ($_.Group | Measure-Object -Property 'Length' -Sum).Sum } `
        } `
    | Sort-Object -Property 'Extension';

That retrieves the contents of $path, filters it to only include files, groups the files by the Extension property, projects each group into an object with a property for the extension and a property for the total file size, then sorts the results by extension.

Upvotes: 2

Related Questions