从给定的位置使用PowerShell我想要validation的文件夹,并显示特定文件types的数量各自的文件夹。 我试图使用该命令来计算一个文件夹中的文件数量,我可以得到在指定的位置可用的文件总数。 我试过这个:
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分配给一个variables,然后testing该variables的值是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!" }
尝试这个。 您可以添加任意数量的文件夹,文件types和文件计数到可放大的$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