question

beginner716-0994 avatar image
0 Votes"
beginner716-0994 asked beginner716-0994 commented

PowerShell Monitor and Run

How to monitor a folder for a new sub-folder created and then monitor the new subfolder for a file for a specific timeframe? When the file is detected within the timeframe, run another file in a different drive and exit. If the file is not detected within the timeframe, then exit?


$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'Z:\abc'
$watcher.Filter ='.'
IncludeSubdirectories = $true
$watcher.Filter = 'xyz.txt'
$watcher.EnableRaisingEvents = $true

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -action {
$path =$event.SourceEventArgs.FullPath
$changeType + $Event.SourceEventsArgs. ChangeType

...STUCK HERE

windows-server-powershelloffice-js-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered beginner716-0994 commented

Here's an example of using FileSystemWatcher. You can modify the code to stay in the do/while for only a certain period by 'remembering' the time you entered the loop and subtracting that from the current time after each 'sleep'. If the resulting time difference is less than the period you define, stay in the loop, otherwise use a 'break' to exit the loop.

using-filesystemwatcher-correctly-part-2


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much

0 Votes 0 ·