Polling for file change inside a folder and then taking the new file and moving

I need a good sample code that will allow me to monitor a folder for ANY changes to a given file and then once that change takes place, move that file to an alternate location.

The challenging part here for me is the polling code.

Anyone please assist me.

Thank you

I built a solution for almost this exact scenario at my work. I use it to push important code out to teammates computers so it’s easy to stay on the same version. This is the article I used as an example. Hope this helps.

Logan, that Filter limitation is going to kill me. I need a solution that will track a file with regard to its file extension specifically.

 

Any ideas?

Im not certain what you mean by filter. Can you show me with an example of where you are stuck? Personally, I use a RegEx Switch statement to handle the $Event.SourceEventArgs.ChangeType, that allows me to customize what happens for the different change types I am interested in, renamed, change, create, and deleted. From my switch I do additional verification and manipulations because the objects have different properties depending on the event. You can do filtering on file extensions based on the $Event.SourceEventArgs.Name I would imagine. Does that help?

Logan:

Any sample code you can share for simply watching a folder for file extension changes, and when they occur, moving them to another location on the network?

 

Would be much appreciated my friend.

 

Thank you in advance.

I do not have an example specifically for when a file extension changes. That should occur under a “Changed” event type. The link I shared has example code and a nice function at the end. The magic is in the scriptblock you define as the argument for the -Action parameter (if using the filesystemwatcher function from that MCP article). I am happy to help debug some of your code if you share an example. If you want me to write the code my consulting rates start at $1500 and that buys you up to 20 hours.

By Filter I mean in the article where it states

The Filter property is basically just specifying what kind of items you are looking for. It only allows for a single string to be used and you cannot use something like “.txt|.xls” as it is not supported.

 

Forget about file extensions then Logan. Do you just have some sample code that will monitor a file folder for changes such as a new file appearing and then moving it to another place in the network such as a shared network drive?

Will the code in that article accomplish such a task?

Please respond ASAP if possible.

 

Thank you Sir for all your help thus far!!!

At this point I am quite convinced you merely skimmed the article rather than taking time to read and understand it. That is unfortunate because you would have seen the code that can be used as an example had you taken the time. Boe Prox is highly respected in this community and if you are serious about learning PowerShell you need to put in the time and effort. I did you a favor and copied the code from Boe’s and pasted it below. This is a working example of the FileSystemWatcher in action. I changed the folder path on line 74, and updated the event types in line 100 to “Changed”,“Renamed”. The meat and potatoes of the code remains unchanged from the article. For further assistance please demonstrate you are serious about learning this stuff. It takes an incredible amount of time and energy to develop these solutions to be handing them out for free. Especially to those who seem unwilling to help themselves.

Function Start-FileSystemWatcher  {
  [cmdletbinding()]
  Param (
  [parameter()]
  [string]$Path,
  [parameter()]
  [ValidateSet('Changed','Created','Deleted','Renamed')]
  [string[]]$EventName,
  [parameter()]
  [string]$Filter,
  [parameter()]
  [System.IO.NotifyFilters]$NotifyFilter,
  [parameter()]
  [switch]$Recurse,
  [parameter()]
  [scriptblock]$Action
  )
  #region Build  FileSystemWatcher
  $FileSystemWatcher  = New-Object  System.IO.FileSystemWatcher
  If (-NOT $PSBoundParameters.ContainsKey('Path')){
  $Path  = $PWD
  }
  $FileSystemWatcher.Path = $Path
  If ($PSBoundParameters.ContainsKey('Filter')) {
  $FileSystemWatcher.Filter = $Filter
  }
  If ($PSBoundParameters.ContainsKey('NotifyFilter')) {
  $FileSystemWatcher.NotifyFilter =  $NotifyFilter
  }
  If ($PSBoundParameters.ContainsKey('Recurse')) {
  $FileSystemWatcher.IncludeSubdirectories =  $True
  }
  If (-NOT $PSBoundParameters.ContainsKey('EventName')){
  $EventName  = 'Changed','Created','Deleted','Renamed'
  }
  If (-NOT $PSBoundParameters.ContainsKey('Action')){
  $Action  = {
  Switch  ($Event.SourceEventArgs.ChangeType) {
  'Renamed'  {
  $Object  = "{0} was  {1} to {2} at {3}" -f $Event.SourceArgs[-1].OldFullPath,
  $Event.SourceEventArgs.ChangeType,
  $Event.SourceArgs[-1].FullPath,
  $Event.TimeGenerated
  }
  Default  {
  $Object  = "{0} was  {1} at {2}" -f $Event.SourceEventArgs.FullPath,
  $Event.SourceEventArgs.ChangeType,
  $Event.TimeGenerated
  }
  }
  $WriteHostParams  = @{
  ForegroundColor = 'Green'
  BackgroundColor = 'Black'
  Object =  $Object
  }
  Write-Host  @WriteHostParams
  }
  }
#endregion  Build FileSystemWatcher
    #region  Initiate Jobs for FileSystemWatcher
  $ObjectEventParams  = @{
  InputObject =  $FileSystemWatcher
  Action =  $Action
  }
  ForEach  ($Item in  $EventName) {
  $ObjectEventParams.EventName = $Item
  $ObjectEventParams.SourceIdentifier =  "File.$($Item)"
  Write-Verbose  "Starting watcher for Event: $($Item)"
  $Null  = Register-ObjectEvent  @ObjectEventParams
  }
  #endregion  Initiate Jobs for FileSystemWatcher
} 
$FileSystemWatcherParams = @{
  Path = 'C:\temp'
  Recurse =  $True
  NotifyFilter =  'FileName'
  Verbose =  $True
  Action=  {
  $Item  = Get-Item $Event.SourceEventArgs.FullPath
  $WriteHostParams  = @{
  ForegroundColor = 'Green'
  BackgroundColor = 'Black'
  }            
  Switch  -regex ($Item.Extension) {
  '\.(ps1|psm1|psd1)'  {$WriteHostParams.Object  = "Processing  PowerShell file: $($Item.Name)"}
  '\.(docx|doc)'  {$WriteHostParams.Object  = "Processing  Word document: $($Item.Name)"}
  '\.(xlsx|xls)'  {$WriteHostParams.Object  = "Processing  Excel spreadsheet: $($Item.Name)"}
  '\.csv'  {$WriteHostParams.Object  = "Processing  CSV spreadsheet: $($Item.Name)"}
  '\.xml'  {$WriteHostParams.Object  = "Processing  XML document: $($Item.Name)"}
  '\.exe'  {$WriteHostParams.Object  = "Processing  Executable: $($Item.Name)"}
  '\.onepkg'  {$WriteHostParams.Object  = "Processing  OneNote package: $($Item.Name)"}
  '\.lnk'  {$WriteHostParams.Object  = "Processing  Link: $($Item.Name)"}
  '\.cer|\.pfx'  {$WriteHostParams.Object  = "Processing  Certificate File: $($Item.Name)"}
  Default{$WriteHostParams.Object  = "Processing  File: $($Item.Name)"}
  }   
  $Item  | Remove-Item
  Write-Host  @WriteHostParams 
  }
}
@('changed','Renamed') | ForEach {
  $FileSystemWatcherParams.EventName = $_
  Start-FileSystemWatcher  @FileSystemWatcherParams
}

 

Logan, I am truly convinced… You are NOT A HUMAN BEING!!! Your an ANGEL IN DISGUISE!!!

Something is definitely wrong with your profile picture because I cannot see your halo and your wings in the picture. Whats up with that?

All kidding aside thank you so much for this code.

One question though. Lets pretend that the folder being watched is the C:\Temp folder and any changes to it, (addition of new files as an example), show up and are re routed to the Z:\Active network share.

 

Could you modify this code with those particular specs please?

 

Thank you again so much Mr. Angel in Disguise!!! hehe

Thank you for the compliments. It has taken me 7 years to get to this point. Thousands of hours studying with literally over a million lines of code written. If I do the modifications for you then I would be writing the code, and I don’t create solutions for free. The best I can do is direct you to line 96 in the example. That’s an excellent place for you to start experimenting to achieve your objectives. Best of luck to you.

 

Thanks again for all your help. I truly appreciate everything you shared with me.

 

Have a blessed Friday and Weekend Sir

Got it. Thanks again