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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00079.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2016/2/16 6:07:13
冷日因為某些因素,需要用 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』?不過她後面還是說數字不同?真心覺得應該要來實驗看看...
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

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