0

I am trying to build a FileWatcher program in Windows PowerShell. I have 4 locations which I need to monitor. In those 4 locations I already have hundreds of files.

I have a specific format for the file which I receive; e.g. the file name begins with yesterday's date, i.e. if today's date is 2018-11-19 then the file which I need to monitor starts with 20181118[5 digits]_ABC_XYZ.csv (I think that I would need to use regular expressions here).

After I receive the file I need to check whether the file is in that particular format and if it is then I need to copy the file at a specified place. The file may come at any time.

Following is the program code which I could test till now:

$Date = (Get-Date).AddDays(-1)

$DateStr = $Date.ToString("yyyyMMdd")

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\source"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $Name = $Event.SourceEventArgs.Name
                $Extension = $Event.SourceEventArgs.Name.extension
                $logline = "$(Get-Date), $changeType, $path, $Name, $Extension"
                Add-content "D:\log.txt" -value $logline                
              }    


### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    while ($true) {sleep 5}

I understand that I need to write the code in $action block to check for the format of the file I receive and is the file is in the proper format then I need to copy the file to another location.

Please kindly guide me in checking for the format of the file in the $action block and further copying the file. Also how do I monitor 4 locations? Do I give 4 paths in $watcher.Path separated by commas?

Akaizoku
  • 456
  • 5
  • 19
user10675448
  • 23
  • 1
  • 5
  • Each file location will need a separate watcher unless they're all under the same subdirectory in some way in which case you can set up a single watcher to recursively search. Note there are some limitations with the `FileSystemWatcher` class where the buffer overflowing can cause situations where it will stop working. – Maximilian Burszley Nov 19 '18 at 16:06
  • 1
    Additionally, the `FileSystemWatcher` constructor can take arguments for path/filter so you can simplify your code: `$watcher = New-Object -TypeName System.IO.FileSystemWatcher -ArgumentList C:\source, *.*` (or on psv5+: `$watcher = [IO.FileSystemWatcher]::new('C:\source', '*.*')` – Maximilian Burszley Nov 19 '18 at 16:09
  • My experience is the watcher comes with some caveats and can miss updates. Do you need this real-time, or can you poll for changes relative to a baseline on another timer? Are there a large number of files in the directories to be monitored? Depending on what you really want to accomplish, different methods could meet your requirements. – Kory Gill Nov 19 '18 at 23:30
  • I have managed to check the format of the file in the $action block ... copying the file should not be a problem ... The only issue which i am facing now is how to monitor around 6 locations using the same File Watcher .... need help for the same .... – user10675448 Nov 20 '18 at 15:53

0 Answers0