茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670872 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00005.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [分享]用 PowerShell 來檢查檔案的行數

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]用 PowerShell 來檢查檔案的行數
冷日因為某些因素,需要用 PowerShell 來檢查檔案的行數!
一開始冷日是這樣寫的:
$FileContent = Get-Content $File2Compare
$LineCount = $FileContent.count

本來運作都很正常,可某天被客戶發現『當檔案內容只有一行時會出錯』!
註:嚴格來說應該是一行資料與一個空白行,若用一般文字編輯器打開,前面的 Line Counter 都會顯示「2」

經檢查發現,冷日的寫法會在上述狀況(檔案只有一行內容與一個換行空白行時程式會出錯)時 $LineCount 變成 NULL!
找半天都找不到真正的原因,難道是 M$ PowerShell 的 Get-Content Properties 有問題?這沒道理阿!?

後來找到這篇:
For what it's worth, I found the above example returned the wrong number of lines. I found this returned the correct count:
$measure = Get-Content c:\yourfile.xyz | Measure-Object
$lines = $measure.Count
echo "line count is: ${lines}"

You probably want to test both methods to figure out what gives you the answer you want. Using "Line" returned 20 and "Count" returned 24. The file contained 24 lines.

原文出處:loops - Powershell - Count lines in file and store in Variable - Stack Overflow

看起來這個 Count 要加上『Measure-Object』?不過她後面還是說數字不同?真心覺得應該要來實驗看看...
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|