Polling Folder Workflow

So I have an excellent polling program set up that will report on several different file states such as delete, copy, rename, etc.

I need to create a workflow that will do the following:

  1. Poll a selected folder one time for a predetermined period of time and then stop. ( I will have the entire script repeat which I will set up later ).
2. Perform a Move-Item of the item that was changed, (if any), and then repeat the entire script again at a specified time which I need to define as well.

 

Any help with this would be definitely appreciated. Here is what I have so far:

 

 

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"

Move-Item -Path C:\temp\*.xml -Destination C:\temp1

Again i ask that you please read the formatting guide and edit your post accordingly.

https://powershell.org/forums/topic/guide-to-posting-code-2/

Apologies. Will do.

 

Looks much better. Thanks!

Yes Sir. Again profoundest apologies Sir!!!