Powershell
使用 powershell ;從文件夾的給定位置,我想驗證並顯示特定文件類型的數量
從文件夾的給定位置使用 powershell 我想驗證並顯示各個文件夾的特定文件類型的數量。我嘗試使用該命令來計算文件夾中的文件數,我可以獲得指定位置中可用文件的總數。我試過這個:
Write-Host ( Get-ChildItem -filter '*cab' 'C:\Users\praveen\Desktop\Package _Sprint04\Sprint04\lfp\Niagara\hpgl2\win2k_xp_vista').Count if (Get-Process | ?{ $Count -eq "13"}) { Write-Host "Number of CAB files are right!" } else { Write-Host "Incorrect!! number of CAB file" }
Get-Process
不會讓你到任何地方。將 分配Count
給一個變數並測試該變數的值是否為 13:$cabFileCount = (Get-ChildItem -Filter "*.cab" "C:\path\to\folder").Count Write-Host $cabFileCount if($cabFileCount -eq 13){ # Success! Write-Host "$cabFileCount files found, perfect!" } else { # Failure! Write-Host "$cabFileCount files found, incorrect!" }
試試這個。您可以將任意數量的文件夾、文件類型和文件計數添加到 vairable
$FoldersToCheck
:# File to store log $LogFile = '.\FileCount.log' $FoldersToCheck = @( @{ Path = 'C:\path\to\folder' FileType = '*.cab' FileCount = 13 }, @{ Path = 'C:\path\to\folder\subfolder' FileType = '*.txt' FileCount = 14 }, @{ Path = 'D:\path\to\some\other\folder' FileType = '*.log' FileCount = 15 } # ... etc, add more hashtables for other folders ) $FoldersToCheck | ForEach-Object { $FileCount = (Get-ChildItem -LiteralPath $_.Path -Filter $_.FileType | Where-Object {!($_.PSIsContainer)}).Count if ($FileCount -eq $_.FileCount) { $Result = "Success! Expected $($_.FileCount) file(s) of type $($_.FileType) in folder $($_.Path), found $FileCount files" } else { $Result = "Failure! Expected $($_.FileCount) file(s) of type $($_.FileType) in folder $($_.Path), found $FileCount files" } # Output result to file and pipeline $Result | Tee-Object -LiteralPath $LogFile }
樣本輸出:
Success! Expected 13 file(s) of type *.cab in folder C:\path\to\folder, found 13 files Failure! Expected 14 file(s) of type *.txt in folder C:\path\to\folder\subfolder, found 10 files Failure! Expected 15 file(s) of type *.log in folder D:\path\to\some\other\folder, found 18 files