PoshCode Archive  Artifact Content

Artifact 95d852cd8ea011c78f05ab538d7347614d2d47b8569887536cf864f13f30cf36:

  • File new-get-childitem.ps1 — part of check-in [ae6ca2d071] at 2018-06-10 13:19:59 on branch trunk — proxy-function to get-childitem (user: walid toumi size: 5311)

# encoding: utf-8
# api: powershell
# title: new get-childitem
# description: proxy-function to get-childitem
# version: 0.1
# type: function
# author: walid toumi
# license: CC0
# function: Get-ChildItem
# x-poshcode-id: 3072
# x-archived: 2012-01-14T07:04:28
# x-published: 2012-11-26T20:39:00
#
# usage:
# ———-
# PS D:\Documents and Settings\walid2mi> Get-ChildItem -Pattern “\.”
# Rpertoire : D:\Documents and Settings\walid2mi
# Mode                LastWriteTime     Length Name                                    
# ——                ——————-     ——— ——                                    
# a—        21/11/2010     11:28          6 k.txt                                   
# a—        04/11/2010     10:49       1362 t.txt                                   
# a—        05/11/2010     06:14       3070 w.txt                                   
# PS D:\Documents and Settings\walid2mi> Get-ChildItem -Pattern “\.” -BinarySize
# Rpertoire : D:\Documents and Settings\walid2mi
# Mode                LastWriteTime     Length Name                                    
# ——                ——————-     ——— ——                                    
# a—        21/11/2010     11:28         6B k.txt                                   
# a—        04/11/2010     10:49        1KB t.txt                                   
# a—        05/11/2010     06:14        3KB w.txt                                   
# PS D:\Documents and Settings\walid2mi> Get-ChildItem -Pattern “\.” -BinarySize | sort -Descending size
# Rpertoire : D:\Documents and Settings\walid2mi
# Mode                LastWriteTime     Length Name                                    
# ——                ——————-     ——— ——                                    
# a—        05/11/2010     06:14        3KB w.txt                                   
# a—        04/11/2010     10:49        1KB t.txt                                   
# a—        21/11/2010     11:28         6B k.txt
#
Function Get-ChildItem {
<#

.ForwardHelpTargetName Get-ChildItem
.ForwardHelpCategory Cmdlet

#>

[CmdletBinding(DefaultParameterSetName='Items', SupportsTransactions=$true)]
param(
    [Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.String[]]
    ${Path},

    [Parameter(ParameterSetName='LiteralItems', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [Alias('PSPath')]
    [System.String[]]
    ${LiteralPath},

    [Parameter(Position=1)]
    [System.String]
    ${Filter},

    [System.String[]]
    ${Include},

    [System.String[]]
    ${Exclude},
    
    [System.String]
    ${Pattern},

    [Switch]
    ${Recurse},

    [Switch]
    ${BinarySizeInHumanReadableFormat},

    [Switch]
    ${Force},

    [Switch]
    ${Name})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet)
        $cmd = ""
        if($BinarySizeInHumanReadableFormat) {
          $PSBoundParameters.Remove('BinarySizeInHumanReadableFormat') | Out-Null
          $cmd = @"
            | ForEach-Object {
                 `$_length=Switch(`$_.length) {
                  { `$_ -lt 1kb } 
                           {  '{0}B' -f (`$_) ;break }
                  { `$_ -lt 1MB }
                           {  '{0}KB' -f ([math]::round(`$(`$_/ 1kb)), 2) ;break }
                  { `$_ -lt 1gb }
                            { '{0}MB' -f ([math]::round(`$(`$_/ 1mb), 2)) ;break }
                  defaut { 
                            {  '{0}GB' -f ([math]::round(`$(`$_/ 1gb), 2)) ;break }
                   }
                }
                if(`$_.PSISContainer) { `$_length=`$null }
                 `$_ | Add-Member noteproperty size `$_.length -Pass |  
                    Add-Member noteproperty length `$_length -PassThru -Force              
            }         
"@
        }
        if($PSBoundParameters['Pattern']) {
          if($Filter -or $Include) {
           throw "les paramètres Pattern et Filter/Include sont mutuellemnt exculsive"
          } else {
          $PSBoundParameters.Remove('Pattern') | Out-Null
          $scriptCmd = {& $wrappedCmd @PSBoundParameters | Where { $_.Name -imatch "$Pattern"  } }
          }
        } else {
          $scriptCmd = {& $wrappedCmd @PSBoundParameters } 
        }
        $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
                $scriptCmd.ToString() + $cmd
            )
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}

}