Author Posts

July 11, 2018 at 4:44 am

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.

July 11, 2018 at 6:31 am

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

July 15, 2018 at 1:38 am

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',...