2010-11-08 69 views
5

我有Get-ChildItem的結果,我想遍歷這些,並顯示它們的名稱。默認情況下,如果我只是使用Write-Host然後我得到它沿行這樣羅列出來:如何編寫按列按列編排順序排列的列表?

PerfLogs Program Files Program Files (x86) Python31 Temp Users Windows 

然而,說,我知道我想要它分成X列,我希望輸出這樣的代替:

PerfLogs     Python31  Windows 
Program Files    Temp 
Program Files (x86)  Users 

正如您所看到的,它首先在列中列出,然後跨過列。

任何想法如何得到這樣的輸出?理想情況下,它將使用最適合屏幕的列數,並且每列中的名稱都對齊左側。

更新:感謝羅馬,我現在可以有我的Linux風格與目錄顏色的'ls'輸出。建立了他的更新後的腳本我有:

function color-ls 
{ 
    dir $args | Format-High -Print {  
     $item = $args 
     $fore = $host.UI.RawUI.ForegroundColor   
     $host.UI.RawUI.ForegroundColor = .{  
      if ($item[1].psIsContainer) {'Blue'} 
      elseif ($item[1].Extension -match '\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)') {'Red'} 
      elseif ($item[1].Extension -match '\.(zip|tar|gz|rar)') {'Yellow'} 
      elseif ($item[1].Extension -match '\.(py|pl|cs|rb|h|cpp)') {'Cyan'} 
      elseif ($item[1].Extension -match '\.(txt|cfg|conf|ini|csv|log|xml)') {'Green'} 
      else {$fore} 
     } 
     write-host $args[0] -NoNewLine 
     $host.UI.RawUI.ForegroundColor = $fore 
    } 
} 

輸出:

http://dl.dropbox.com/u/2809/lscolor.png

+0

我很高興你在某種程度上,它是專爲精確地重複使用更新後的腳本。感謝你的鼓舞人心的想法。這將是很好,如果PowerShell有這個內置的,我已經提交了建議: https://connect.microsoft.com/PowerShell/feedback/details/620452/format-wide-add-an-option-to-output逐列 – 2010-11-10 05:30:24

回答

6

這是一個有趣的想法和任務。

更新:更新的腳本包含一些修復和改進。它還允許以多種方式自定義輸出。請參閱腳本註釋中的示例。

腳本格式High.ps1:

<# 
.SYNOPSIS 
    Formats input by columns using maximum suitable column number. 

.DESCRIPTION 
    Format-High prints the specified property, expression, or string 
    representation of input objects filling the table by columns. 

    It is named in contrast to Format-Wide which prints by rows. 

.EXAMPLE 
    # just items 
    ls c:\windows | Format-High 

    # ditto in colors based on PSIsContainer 
    ls c:\windows | Format-High -Print {$c = if ($args[1].PSIsContainer) {'yellow'} else {'white'}; Write-Host $args[0] -ForegroundColor $c -NoNewline} 

    # just processes, not good 
    ps | Format-High 

    # process names, much better 
    ps | Format-High Name 

    # custom expression and width 
    ps | Format-High {$_.Name + ':' + $_.WS} 70 

    # process names in colors based on working sets 
    ps | Format-High Name 70 {$c = if ($args[1].WS -gt 10mb) {'red'} else {'green'}; Write-Host $args[0] -ForegroundColor $c -NoNewline} 
#> 

param 
(
    [object]$Property, 
    [int]$Width = $Host.UI.RawUI.WindowSize.Width - 1, 
    [scriptblock]$Print = { Write-Host $args[0] -NoNewline }, 
    [object[]]$InputObject 
) 

# process the input, get strings to format 
if ($InputObject -eq $null) { $InputObject = @($input) } 
if ($Property -is [string]) { $strings = $InputObject | Select-Object -ExpandProperty $Property } 
elseif ($Property -is [scriptblock]) { $strings = $InputObject | ForEach-Object $Property } 
else { $strings = $InputObject } 
$strings = @(foreach($_ in $strings) { "$_" }) 

# pass 1: find the maximum column number 
$nbest = 1 
$bestwidths = @($Width) 
for($ncolumn = 2; ; ++$ncolumn) { 
    $nrow = [Math]::Ceiling($strings.Count/$ncolumn) 
    $widths = @(
     for($s = 0; $s -lt $strings.Count; $s += $nrow) { 
      $e = [Math]::Min($strings.Count, $s + $nrow) 
      ($strings[$s .. ($e - 1)] | Measure-Object -Maximum Length).Maximum + 1 
     } 
    ) 
    if (($widths | Measure-Object -Sum).Sum -gt $Width) { 
     break 
    } 
    $bestwidths = $widths 
    $nbest = $ncolumn 
    if ($nrow -le 1) { 
     break 
    } 
} 

# pass 2: print strings 
$nrow = [Math]::Ceiling($strings.Count/$nbest) 
for($r = 0; $r -lt $nrow; ++$r) { 
    for($c = 0; $c -lt $nbest; ++$c) { 
     $i = $c * $nrow + $r 
     if ($i -lt $strings.Count) { 
      & $Print ($strings[$i].PadRight($bestwidths[$c])) $InputObject[$i] 
     } 
    } 
    & $Print "`r`n" 
} 
+0

同意排序,這是該死的好。我只是在學習PowerShell,你能指點一下。{}在$ widths =中做了什麼。{}? – esac 2010-11-08 20:53:50

+0

在寬度總和後,應該是$ bestwidths =($ widths)..如果只有一個文件,它會拋出一個異常。 – esac 2010-11-08 22:55:45

+0

'。{}'是* dot *操作符(在當前範圍內調用)+由它調用的腳本塊。但是在這種情況下,有一個更好的構造'@()',它也修復了您注意到的錯誤。我會盡快更新代碼(還有其他一些小缺陷)。 – 2010-11-09 05:16:31