2017-09-18 59 views
1

我想提到以下PowerShell來過濾文件大小

$FileExists1 = 'C:\Users\iskandar\Desktop\TEST\*\test0700.txt' 

IF (Test-Path $FileExists1) { 
    If ((Get-Item $FileExists1).length -gt 0kb) { 
     Write-Output "All file size is good!" 
    } 

Else { 
     Write-Output " There is a file with 0KB, Please inform support staff" 
    } 
} 

如果有多個文件夾多,腳本無法過濾0KB文件編寫使用PowerShell來獲取文件大小0KB腳本。

例如:可以說我有30個文件,只有一個文件和0KB,當我運行這些腳本時,它顯示「所有文件大小都很好!」。

任何人都可以建議嗎?謝謝

回答

0

試試這個:

$files=gci "C:\Users\iskandar\Desktop\TEST" -file -Recurse | where Length -le 0Kb | select -First 1 

if ($files.Count -gt 0) 
{ 
    "There is a file with 0KB, Please inform support staff" 
} 
else 
{ 
    "All file size is good!" 
} 
0

雖然Esperento57提供了一個可行的解決方案,讓我們來看看什麼是錯的原代碼。

問題發生在您遇到對象不匹配時。多個對象具有包含不同內容的相同屬性。

對於單個文件,Get-Item將返回一個FileInfo對象,該對象在其.Length屬性中包含文件大小。

對於多個文件,Get-Item將返回一個FileInfo對象的數組。它的.Length屬性包含數組本身的長度。因此,聲明

If ((Get-Item $FileExists1).length -gt 0kb) { 

實際上被處理成僞

if ($FileExists1-the-array contains more than 0 elements) {