I have created code that will allow for a folder being polled constantly, to monitor for changes in a file such as copy, delete, rename, etc.
I have also included code for a Wait-Function that I am trying to get working. The problem is that I need the script to only poll for a limited period of time and then take the changed files, if any, and move them to a different drive/ shared folder on the network.
Here is what I have so far. Any help would definitely be appreciated.
Thank you.
Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) Function Register-Watcher { param ($folder) $filter = "*.*" #all files $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $false EnableRaisingEvents = $true } $changeAction = [scriptblock]::Create(' # This is the code which will be executed every time a file change is detected $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file $name was $changeType at $timeStamp" ') $changeAction2 = [scriptblock]::Create(' # This is the code which will be executed every time a file change is detected $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file $name was $changeType at $timeStamp" ') $changeAction3 = [scriptblock]::Create(' # This is the code which will be executed every time a file change is detected $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file $name was $changeType at $timeStamp" ') $changeAction4 = [scriptblock]::Create(' # This is the code which will be executed every time a file change is detected $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file $name was $changeType at $timeStamp" ') Register-ObjectEvent $Watcher -EventName "Changed" -Action $changeAction Register-ObjectEvent $Watcher -EventName "Created" -Action $changeAction2 Register-ObjectEvent $Watcher -EventName "Deleted" -Action $changeAction3 Register-ObjectEvent $Watcher -EventName "Renamed" -Action $changeAction4 } Register-Watcher "c:\temp" function Wait-Action { [OutputType([void])] [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [scriptblock]$Condition, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [int]$Timeout, [Parameter()] [ValidateNotNullOrEmpty()] [object[]]$ArgumentList, [Parameter()] [ValidateNotNullOrEmpty()] [int]$RetryInterval = 5 ) try { $timer = [Diagnostics.Stopwatch]::StartNew() while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and (-not (& $Condition $ArgumentList)) { Start-Sleep -Seconds $RetryInterval $totalSecs = [math]::Round($timer.Elapsed.TotalSeconds, 0) Write-Verbose -Message "Still waiting for action to complete after [$totalSecs] seconds..." } $timer.Stop() if ($timer.Elapsed.TotalSeconds -gt $Timeout) { throw 'Action did not complete before timeout period.' } else { Write-Verbose -Message 'Action completed before timeout period.' } } catch { Write-Error -Message $_.Exception.Message } } Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) Wait-Action -Condition {Test-Path -Path 'c:\*.xml' -PathType Leaf} -Timeout 60 -$RetryInterval 1; Move-Item -Path C:\temp\*.xml -Destination C:\temp1