0

Using powershell from a given location a of folder I want to validate and show the number of specific filetypes respective folder. I tried using the command to count the number of files in a folder I am able to get total count of files avaiable in the specified location. I have tried this:

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"
}
Daniel
  • 6,940
  • 6
  • 33
  • 64

2 Answers2

2

Get-Process won't get you anywhere. Assign the Count to a variable and test if the value of that variable is then 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!"
}
Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95
  • Thanks !!! @Mathias am able to run the given code successfully but i cant proceed with my Question of counting the files in each folder separately. Ex: i have folder with sub-folders in it ,the sub-folders contains 13 files . now i want to validate the number of files in sub-folders, then write out the output to a file. I have been stuck here for a long time . – Praveen Kambar Mar 11 '15 at 08:31
  • You'd better use `Get-ChildItem -Directory` or pipe `Get-ChildItem` output to the `Where-Object {!($_.PSIsContainer)}`. Because `BlaBlaBla.cab` is a perfectly valid folder name. Trust no one ;). – beatcracker Mar 16 '15 at 11:48
0

Try this. You can add any amount of folders, file types and file counts to the 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
}

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
beatcracker
  • 1,359
  • 8
  • 13
  • Thanks Beatcracker.but the problem here is i dont want to give the location of each subfolder manually.i dont wanna use Hash Tables. – Praveen Kambar Mar 17 '15 at 05:14
  • So you want to count files in every subfolder? That can be done easily, but the thing is, you have to store somewhere count of files to compare to. It means you still have to use some sort of variable. Or there would be `13` files in every subfolder, no matter of what? – beatcracker Mar 17 '15 at 10:59
  • i have got this code which counts number of files in the whole directory. – Praveen Kambar Mar 17 '15 at 11:22
  • yea can u pls share the code for counting number of files in a directory of each folder but the condition is that the files should be of specific name(String). – Praveen Kambar Mar 18 '15 at 04:17