|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1730027
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2016/2/16 6:07 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15773
|
- [分享]用 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』?不過她後面還是說數字不同?真心覺得應該要來實驗看看...
|
|
|
|