использование powershell; от данного местоположения папки я хочу проверить и показать количество определенных типов файлов соответствующая папка

Используя powershell от данного местоположения папки я хочу проверить и показать количество определенных типов файлов соответствующая папка. Я пытался использовать команду для подсчета количества файлов в папке, я могу получить общее количество файлов, avaiable в указанном месте. Я попробовал это:

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"
}
0
задан 11 March 2015 в 09:25
2 ответа

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!"
}
2
ответ дан 4 December 2019 в 13:51

Попробуй это. Вы можете добавить любое количество папок, типов файлов и количества файлов в переменную $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
}

Sample output:

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
0
ответ дан 4 December 2019 в 13:51

Теги

Похожие вопросы