2010-11-08 9 views
5

मैं Get-ChildItem का परिणाम है, और मुझे इन पर पुनरावृति, और उनके नाम प्रदर्शित करना चाहते हैं। डिफ़ॉल्ट रूप से करता है, तो मैं बस तो Write-Host का उपयोग मैं इसे इस तरह पंक्ति के साथ बाहर सूचीबद्ध मिलती है:कैसे कॉलम के आधार पर सूचीबद्ध एक ग्रिड में कोषगत अनुसार क्रमबद्ध एक सूची लिखने के लिए?

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

हालांकि, का कहना है कि मुझे पता है कि मैं इसे एक्स कॉलम में विभाजित करना चाहते हैं, मैं इस तरह उत्पादन के बजाय हैं:

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 -by-column –

उत्तर

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

सॉर्टिंग पर सहमति हुई, यह बहुत अच्छा है। मैं केवल पावरहेल सीख रहा हूं, क्या आप मुझे बता सकते हैं कि {।} $ चौड़ाई = {} में क्या करता है? चौड़ाई के सारांश के बाद – esac

+0

, यह $ bestwidths = ($ widths) होना चाहिए .. यदि केवल एक फ़ाइल है तो यह अपवाद फेंकता है। – esac

+0

'। {}' * डॉट * ऑपरेटर (वर्तमान दायरे में आक्रमण) है + स्क्रिप्ट ब्लॉक इसके द्वारा बुलाया जा रहा है। लेकिन इस मामले में एक बेहतर निर्माण '@() 'है जो आपके द्वारा देखी गई बग को भी ठीक करता है। मैं जल्द ही कोड अपडेट करूंगा (अन्य मामूली खामियां भी हैं)। –

संबंधित मुद्दे