Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Monitor a folder that the number of files grows and shrinks and create GUI form.

$
0
0

I am trying to create a Powershell script that will do the following:

I need to monitor a folder that the number of files grows and shrinks.

1. Display a GUI form that contains a label that displays the count of the files from a folder. (I have this working)
2. This form also needs a quit button that will close the form. (I have this working)
3. The count label color and font size needs to update automaticaly every 10 seconds.
3. The count of the files, I would like to change the size and color depending on the value. So if it is from 0-10,
color would be green and font size of 10. If between 11-20, color would be black and font size of 14. 21-30, orange
and font of 18 and if count of files is greater that 30, size of 30 and color of red.
4. Additional feature, if it quits shrinking for more than x seconds, the label could blink red font.
5. Another additional feature: If the folder contains more than 50 files for more than 60 seconds, send an email.

I have some of this. It displays the GUI form and quit button. It works displaying the number of files and the quit button works, but it does not update automaticaly.
Here is what I have so far from what I gathered from I/N searches:

#################################################
#.\guicount.ps1
$path = "C:\Files"
$count = Get-ChildItem -Path $path |            
          where {!$_.PSIsContainer} |            
          Measure-Object |           
          select -ExpandProperty count

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 7/3/2011 11:35 AM
# Generated By: sean.kearney
########################################################################
#Need this in order to run: [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$Form = New-Object System.Windows.Forms.Form
$QuitButton = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_QuitButton_Click=
{
#TODO: Place custom script here
#Place code here to quit program.
$Form.close()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$Form.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$Form.Text = "Count of Unprocessed Files"
$Form.Name = "Form"
$Form.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 365
$System_Drawing_Size.Height = 200
$Form.ClientSize = $System_Drawing_Size

$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 80,50
$label.Size = New-Object Drawing.Point 200,15
$label.text = $count

$QuitButton.TabIndex = 0
$QuitButton.Name = "QuitButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 240
$System_Drawing_Size.Height = 23
$QuitButton.Size = New-Object Drawing.Point 100,25
$QuitButton.UseVisualStyleBackColor = $True

$QuitButton.Text = "QUIT"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 13
$QuitButton.Location = $System_Drawing_Point
$QuitButton.DataBindings.DefaultDataSourceUpdateMode = 0
$QuitButton.add_Click($handler_QuitButton_Click)

$Form.Controls.Add($QuitButton)
$Form.Controls.Add($label)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $Form.WindowState
#Init the OnLoad event to correct the initial state of the form
$Form.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$Form.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

#################################################


Viewing all articles
Browse latest Browse all 6937

Trending Articles