How to Alphabetize a Listing?

Welcome Forums General PowerShell Q&A How to Alphabetize a Listing?

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
5 months ago.

  • Author
    Posts
  • #104236

    Participant
    Points: 0
    Rank: Member
    function Get-FilesReadyToArchive
    {
        [CmdletBinding()]
        Param
        (
            # Determine path to head directory of archive tree
            [Parameter(Mandatory=$true, 
                       ValueFromPipeline=$true,
                       ValueFromPipelineByPropertyName=$true)] 
            [string]$PathToArchive
        )
    
        Begin
        {
        }
        Process{$AFiles=Get-ChildItem -Path $PathToArchive -Recurse -File
        
        foreach($file in $Afiles){
            if((Get-ItemProperty -Path $file.FullName).Attributes -band [io.fileattributes]::archive -and (Get-ItemProperty -Path $file.FullName -Exclude *partial*))
            {$hash=[ordered]@{'Directory'=($file).Directory
                        'File Name'=($file).basename
                        'File Type'=($file).Extension
                        'File Size (KB)'=($file).Length/1e3 -as [int]
                        'Date Created'=($file).LastWriteTime}
               
                 $glob=New-Object -TypeName PSObject -Property $hash
                 Write-Output $glob}
                 
            }
    }}
    
    function Save-TableOfContents
    {
        [CmdletBinding()]
        [OutputType([int])]
        Param
        (
            # Param1 help description
            [Parameter(Mandatory=$true,HelpMessage="Default location is C:\MyArchive.html",
                       Position=0)]
                       [string]$PathToContents
    
    
        )
    
        Begin
        {
        }
        Process
        {
    
      
    
           Get-FilesReadyToArchive | Sort-Object -Property directory | ConvertTo-Html -Property 'File Name', 'File Type','File Size (KB)','Date Created' -Head 'Document created on' -PreContent (Get-Date) > $PathToContents
    
    
        }
        End
        {  
        }
    }
    

    Powershell version 5.0; OS Windows 7 Home Premium
    I have written a module that can copy all files in a folder with the archive bit set to archival storage. One annoying problem is when I use the cmdlet that saves a Table of Contents in an HTML file, the file listing is random. Also, if I run it twice, I get a different order for the listing. Is there any way to achieve a listing that is in alphabetical order? Shown are the two cmdlets involved.

  • #104246

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    Based on a recent thread I was on, take look at this article on Sort-Object and see if leveraging expressions will help.

    PowerShell Sort-Object gotcha's
    https://blogs.technet.microsoft.com/stefan_stranger/2013/11/13/powershell-sort-object-gotchas

  • #104521

    Participant
    Points: 0
    Rank: Member

    This worked! Thanks for the link. I never thought to use a hash table to do a two-level sort on separate properties.
    Here is the new code:

     Get-FilesReadyToArchive | Sort-Object -Property @{expression="directory";ascending=$true},@{expression="file name";ascending=$true} | ConvertTo-Html -Property 'Directory', 'File Name',...
    

The topic ‘How to Alphabetize a Listing?’ is closed to new replies.