Link to home
Start Free TrialLog in
Avatar of tecgate
tecgate

asked on

Powershell - Create file from foreach when file counts greater than 250

$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("D:\websphere\logs")

Open in new window


I have a directory called D:\websphere\logs, and D:\websphere\logs has many of sub-directories.   Each sub-directory doesn't any child sub-directory, so  the directory structure is:

D:\websphere\logs
       \aaa
       \bbb
       \ccc
       \ddd
       etc etc

I need to count the files under each sub-directory, and write to a .txt file when the file count of each sub-folder is greater than 250.

I have the script below, but not working really.

foreach ($folder in $f.subfolders)
          {
             Write-Host $((get-childitem $folder.path).count)','$folder.Path

  New-Object -TypeName PSObject -Property @{
                SystemName = $((get-childitem $folder.path).count)
                Reachable = $folder.Path
  
   } | Export-Csv -path D:\websphere\output.txt -Append  

$data = import-csv -path D:\websphere\output.txt -header close, server
$data | where-object{$_.close -gt 250} |format-table -autosize

Open in new window


The count is working fine, but I can't make the next piece (write file count to a .txt file when the file count of each sub-folder is greater than 250).

So, my working segment so far is:

$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("C:\Windows\Media")
foreach ($folder in $f.subfolders){Write-Host $((get-childitem $folder.path).count)','$folder.Path}

Open in new window



Please let me know how to make this piece (write file count to a .txt file when the file count of each sub-folder is greater than 250) working.

Appreciate it!
Avatar of Bob McCoy
Bob McCoy
Flag of United States of America image

What version of PowerShell are you running?  It matters in the approach to this problem.  The approach below requires V3 or later.

This is also being addressed at powershell.com.

#requires -version 3
$root = "C:\Ephemeral"
$threshold = 2  # set level above which you want to report
$folders = Get-ChildItem -Path $root -Directory
$results = foreach ($folder in $folders) {
    [PSCustomObject]@{
        Folder = $folder
        FileCount = (Get-ChildItem -Path (Join-Path -Path $root -ChildPath $folder) -File).Count
    }
}

# Out-Gridviw is used here, but you could do just about any output from this point.
$results | where FileCount -gt $threshold | Out-GridView

Open in new window

Avatar of tecgate
tecgate

ASKER

Thank you so much, Bob!

I am using PS Version 4.    Your script generates an output.

Ultimately, I need to accomplish these two things:

- Sort the output by descending order of the counts so that the largest count is on the top
- Send out e-mail alerts to e-mail recipients via a scheduler

The 2nd question would be another post, but can you please let me know how to sort first?

Appreciate it!
ASKER CERTIFIED SOLUTION
Avatar of Bob McCoy
Bob McCoy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, what outbound email system are you using?
Avatar of tecgate

ASKER

Thank you, Bob!

The sorting works just charming.
I am using MS Outlook.    The alert e-mail will be using all e-mails inside network.

Please advise.
No, what server are you using for outbound email?
Avatar of tecgate

ASKER

OK, I am using exmail.webarp.com (Exchange Server).  Is it what you are asking?

Thank you!
Avatar of tecgate

ASKER

I opened the mail question at #28932187.

Thank you