locked
Best Way to Run Powershell Script when File is Added to a Specific Directory RRS feed

  • Question

  • I need a script to run whenever there is a file added to a directory. What is the best way to accomplish this task?

    To clarify some things, the file is added to the directory using FTP if that matters and currently only one file should be added to that directory in a given day but I would like to plan ahead incase we decide to increase the frequency of execution.

    Thank you ahead of time for your help.

    Friday, May 25, 2012 7:08 PM

Answers

  • someone named BigTeddy wrote this.  If you work in a windows environment it is what you want as an example, it uses the Windows FileWatcher:

    http://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b

    If this is what you are looking for (it is) then please mark this as correct (sry...people with 0 points tend not to mark their stuff).

    #By BigTeddy 05 September 2011 
     
    #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
    #The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
    #The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
    #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
    #You need not subscribe to all three types of event.  All three are shown for example. 
    # Version 1.1 
     
    $folder = 'c:\scripts\test' # Enter the root path you want to monitor. 
    $filter = '*.*'  # You can enter a wildcard filter here. 
     
    # In the following line, you can change 'IncludeSubdirectories to $true if required.                           
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 
     
    # Here, all three events are registerd.  You need only subscribe to events that you need: 
     
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore red 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore white 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    # To stop the monitoring, run the following commands: 
    # Unregister-Event FileDeleted 
    # Unregister-Event FileCreated 
    # Unregister-Event FileChanged

    • Proposed as answer by CountryStyle Friday, May 25, 2012 7:16 PM
    • Marked as answer by ddwinters91 Friday, May 25, 2012 7:17 PM
    • Unmarked as answer by ddwinters91 Friday, May 25, 2012 7:30 PM
    • Marked as answer by ddwinters91 Monday, June 4, 2012 3:11 AM
    Friday, May 25, 2012 7:15 PM
  • I did not mean to lead you to believe the script would directly suit your purpose without modification.  Did you modify it or use it to create your own script?

    IF you are in a Window only environment the FileWatcher is what you want.

    Here is another thread on the fileSystemWatcher:

    http://social.technet.microsoft.com/Forums/en/ITCG/thread/c75c7bbd-4e32-428a-b3dc-815d5c42fd3

    • Marked as answer by Yan Li_ Monday, June 4, 2012 1:42 AM
    Saturday, May 26, 2012 2:35 AM

All replies

  • someone named BigTeddy wrote this.  If you work in a windows environment it is what you want as an example, it uses the Windows FileWatcher:

    http://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b

    If this is what you are looking for (it is) then please mark this as correct (sry...people with 0 points tend not to mark their stuff).

    #By BigTeddy 05 September 2011 
     
    #This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
    #The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
    #The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
    #The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
    #You need not subscribe to all three types of event.  All three are shown for example. 
    # Version 1.1 
     
    $folder = 'c:\scripts\test' # Enter the root path you want to monitor. 
    $filter = '*.*'  # You can enter a wildcard filter here. 
     
    # In the following line, you can change 'IncludeSubdirectories to $true if required.                           
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 
     
    # Here, all three events are registerd.  You need only subscribe to events that you need: 
     
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore red 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action { 
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated 
    Write-Host "The file '$name' was $changeType at $timeStamp" -fore white 
    Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"} 
     
    # To stop the monitoring, run the following commands: 
    # Unregister-Event FileDeleted 
    # Unregister-Event FileCreated 
    # Unregister-Event FileChanged

    • Proposed as answer by CountryStyle Friday, May 25, 2012 7:16 PM
    • Marked as answer by ddwinters91 Friday, May 25, 2012 7:17 PM
    • Unmarked as answer by ddwinters91 Friday, May 25, 2012 7:30 PM
    • Marked as answer by ddwinters91 Monday, June 4, 2012 3:11 AM
    Friday, May 25, 2012 7:15 PM
  • The only problem is that it only registers the event once. The first file created shows up but the rest don't. Also, when a file was just saved to this location is said the file had been deleted.

    Daniel Winters

    Friday, May 25, 2012 7:30 PM
  • I did not mean to lead you to believe the script would directly suit your purpose without modification.  Did you modify it or use it to create your own script?

    IF you are in a Window only environment the FileWatcher is what you want.

    Here is another thread on the fileSystemWatcher:

    http://social.technet.microsoft.com/Forums/en/ITCG/thread/c75c7bbd-4e32-428a-b3dc-815d5c42fd3

    • Marked as answer by Yan Li_ Monday, June 4, 2012 1:42 AM
    Saturday, May 26, 2012 2:35 AM