QZLin

使Powershell ls(Get-ChildItem)命令输出易读的大小

ref: Show human-readable file sizes in the default PowerShell ls command

优化?的MyFileFormat.format.ps1xml

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <SelectionSets>
        <SelectionSet>
            <Name>FileSystemTypes</Name>
            <Types>
                <TypeName>System.IO.DirectoryInfo</TypeName>
                <TypeName>System.IO.FileInfo</TypeName>
            </Types>
        </SelectionSet>
    </SelectionSets>

    <ViewDefinitions>
        <View>
            <Name>children</Name>
            <ViewSelectedBy>
                <SelectionSetName>FileSystemTypes</SelectionSetName>
            </ViewSelectedBy>
            <GroupBy>
                <PropertyName>PSParentPath</PropertyName>
                <CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
            </GroupBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader>
                        <Label>Mode</Label>
                        <Width>7</Width>
                        <Alignment>left</Alignment>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>LastWriteTime</Label>
                        <Width>25</Width>
                        <Alignment>right</Alignment>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>FileSize</Label>
                        <Width>14</Width>
                        <Alignment>right</Alignment>
                    </TableColumnHeader>
                    <TableColumnHeader/>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <Wrap/>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Mode</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <ScriptBlock>
                                    [String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))
                                </ScriptBlock>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>FileSize</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>

优化?的MyTypes.ps1xml

<?xml version="1.0" encoding="utf-8"?>
<Types>
    <Type>
        <Name>System.IO.FileInfo</Name>
        <Members>
            <ScriptProperty>
                <!-- Filesize converts the length to a human readable
                    format (b, kb, mb, gb, tb) -->
                <Name>FileSize</Name>
                <GetScriptBlock>
                    switch ($this.length) {
                        { $_ -gt 1mb } {
                            if ($_ -lt 1gb)     { "{0:n2} MiB " -f ($_ / 1mb) }
                            elseif ($_ -lt 1tb) { "{0:n2} GiB " -f ($_ / 1gb) }
                            else                { "{0:n2} TiB " -f ($_ / 1tb) }
                            break
                        }
                        default {
                            if ($_ -lt 1kb) { "{0}   B " -f $_ }
                            else            { "{0:n2} KiB " -f ($_ / 1kb) }
                            break
                        }
                    }
                </GetScriptBlock>
            </ScriptProperty>
        </Members>
    </Type>
</Types>

posted on 2022-04-12 19:34  QZLin  阅读(155)  评论(0编辑  收藏  举报

导航