Search code examples
powershellfiledatemonitor

I want to monitor the date and time of a file every 30 seconds


I want to monitor the date and time of a file. I wrote the code that do the job as I want but I can't reposition the gui window. I tried all I could find like "start-job" or create a new runspace but I don't get any results in richtextbox. Any suggestion is welcome.

$targetFile = "full path"

# Function - Add Text to RichTextBox
    function Add-RichTextBox{
        [CmdletBinding()]
        param ($text)
        #$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
        $richtext.Text += "$text"
        $richtext.Text += "`n"
    }

# Windows Form    
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'

$Font = New-Object System.Drawing.Font("Tahoma",11)
$Form.Font = $Font
 
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)

$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,220)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
#$StartButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $StartButton
$form.Controls.Add($StartButton)
$StartButton.Add_click({
       while($true){
$lastdate = Get-ChildItem $targetFile
$Date = $lastdate.LastAccessTime.ToString()
Add-RichTextBox "$date"
$richtext.SelectionStart = $richtext.TextLength
$richText.ScrollToCaret()
#$richtext.refresh()
#$form.refresh()
Start-sleep 30
}
})


## the Rich text box
    $richtext = new-object System.Windows.Forms.RichTextBox
    $richtext.Location = New-Object System.Drawing.Point(20,60)
    $richtext.multiline = $true
    $richtext.Name = "Results"
    $richtext.text = "Results:`n"
    $richtext.scrollbars = "Both"
    $richtext.Height  = 120
    $richtext.width = 350
    $richtext.font = new-object system.drawing.font "Lucida Console",10
    $Form.controls.add($richtext)
    

$Form.Add_Shown({$Form.Activate()})

$form.ShowDialog()

Solution

  • Continuing from my comment to use a Timer on your form (if you absolutely do not want a FileSystemWatcher), here's how you can do that:

    $targetFile   = "D:\Test\blah.txt"
    $lastAccessed = (Get-Date)   # a variable to keep track of the last LastAccessTime of the file
    
    # Windows Form    
    $form               = New-Object System.Windows.Forms.Form
    $form.Text          = "Monitor script"
    $form.Size          = New-Object System.Drawing.Size(400,300)
    $form.StartPosition = 'CenterScreen'
    
    $Font      = New-Object System.Drawing.Font("Tahoma",11)
    $form.Font = $Font
    
    # Label
    $label          = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(20,40)
    $label.Size     = New-Object System.Drawing.Size(200,20)
    $label.Text     = (Get-Date).ToString()
    $form.Controls.Add($label)
    
    # Button
    $StartButton          = New-Object System.Windows.Forms.Button
    $StartButton.Location = New-Object System.Drawing.Point(150,200)
    $StartButton.Size     = New-Object System.Drawing.Size(100,33)
    $StartButton.Text     = 'Start'
    $form.Controls.Add($StartButton)
    $StartButton.Add_click({
        $timer.Enabled = $true
        $timer.Start()
    })
    
    
    # RichTextBox
    $richtext            = New-Object System.Windows.Forms.RichTextBox
    $richtext.Location   = New-Object System.Drawing.Point(20,60)
    $richtext.Multiline  = $true
    $richtext.Name       = "Results"
    $richtext.Text       = "Results:`r`n"
    $richtext.ScrollBars = "Both"
    $richtext.Height     = 120
    $richtext.Width      = 350
    $richtext.Font       = New-Object System.Drawing.Font "Lucida Console",10
    $form.Controls.Add($richtext)
    
    # Timer
    $timer = New-Object System.Windows.Forms.Timer 
    $timer.Interval = 30000   # 30 seconds
    $timer.Enabled  = $false  # disabled at first
    $timer.Add_Tick({ 
        $file = Get-Item -Path $targetFile -ErrorAction SilentlyContinue
        # if we can find the file and its last AccessedTime is not 
        # the same as we already stored in variable $lastAccessed
        # use script-scoping here, so $script:lastAccessed instead of $lastAccessed
        if ($file -and $file.LastAccessTime -gt $script:lastAccessed) {
            $richtext.AppendText("$($file.LastAccessTime.ToString())`r`n")
            $script:lastAccessed = $file.LastAccessTime  # remember this new datetime
        }
    })
    
    $form.ShowDialog()
    
    # Important: Clean up
    $timer.Stop()
    $timer.Dispose()
    $richtext.Dispose()
    $form.Dispose()