2014-10-30 54 views
1

我想弄清楚如何合併行計數,獲取添加到每個文件的循環。計數需要放入每個文件的頁腳,因爲它會檢查它。另一個問題是計數需要包括頁眉和頁腳行的添加(即8行+ 1頁眉+ 1頁腳= 10)。我使用的代碼在下面,我知道計算行數的代碼是Get-Content $mypath | Measure-Object -Line | {$linecount = $_.Count},但我不知道如何正確合併它。有什麼建議麼?行數對於多個文件上的每個對象

Get-ChildItem $destinationfolderpath -REcurse -Filter *.txt | ForEach-Object -Begin { $seq = 0 } -Process { 
     $seq++ 
     $seq1 = "{0:D4}" -f $seq; $header="File Sequence Number $seq1" 
     $footer="File Sequence Number $seq1 and Line Count $looplinecount" 
     $header + "`n" + (Get-Content $_.FullName | Out-String) + $footer | Set-Content -Path $_.FullName 
} 

回答

1

所以該文件的內容加載到可變的環內,該變量執行您measure -line,加入2(標題行中的一個,一個用於頁腳行),並丟棄到一個子-expression頁腳...

Get-ChildItem $destinationfolderpath -REcurse -Filter *.txt | ForEach-Object -Begin { $seq = 0 } -Process { 
    $seq++ 
    $seq1 = "{0:D4}" -f $seq 
    $header="File Sequence Number $seq1" 
    $Content=Get-Content $_.FullName | Out-String 
    $footer="File Sequence Number $seq1 and Line Count $(($content|measure -line|select -expand lines)+2)" 
    "$header`n$Content$footer" | Set-Content -Path $_.FullName 
} 
+0

我可能是錯的,但難道不'措施直插|選擇-expand count'必須'措施直插|選擇-expand lines' – Matt 2014-10-30 21:29:55

+0

我只是用OP的本質上是代碼,我甚至沒有再猜測它。我只是編輯了我的答案來糾正這一點。 – TheMadTechnician 2014-10-30 21:33:15

+0

很好,謝謝!我仍然需要弄清楚如何將$ Content格式化爲「{0:D4}」-f,就像$ seq1中的一樣,但這樣更易於管理。 – Sheddy 2014-10-30 23:03:45