Try to search your question here, if you can't find : Ask Any Question Now ?

Perform action after multiple files are created

HomeCategory: stackoverflowPerform action after multiple files are created
Avatarbhawya asked 4 months ago

I’m new to Powershell scripting, and I’m struggling with how to identify when multiple files have been created in order to initiate a process.

Ultimately, I need to wait until a handful of files have been created (this usually occurs within a finite period of time the same time each day). These files are created at separate times. Once all files have been created and are at their final location, I need to perform a separate action with these files. The trouble I’m having is:

  1. Identifying when all files are available
  2. Identifying how to initiate a separate process once these files are available
  3. If necessary,unregistering the events (I plan to run this script each morning…I don’t know how long these events are registered)

I’ve toyed with using the IO.FileSystemWatcher with some success in order to monitor when any individual directory has this file. While this logs appropriately, I don’t know how to consolidate the collection of these files. Possibly a flag? Not sure how to implement. I’ve also considered using Test-Path as a way of checking to see if these files exist — but a drawback with this is that I’d need to periodically run the script (or Sleep) for a pre-defined period of time.

Does anyone have experience with doing something like this? Or possibly provide guidance?

What I’ve tried (with respect to IO.FileSystemWatcher) using test data:

$i=0 
$paths = "C:","Z:"
$fileName='Test'+$Datestr.Trim()+'.txt' 


foreach ($path in $paths)  

{ 

$fsw = New-Object IO.FileSystemWatcher $path, $fileName -Property @{IncludeSubdirectories = $tru;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier "$i+fileCreated" -Action { 

$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$fpath = $Event.SourceEventArgs.FullPath 
$timeStamp = $Event.TimeGenerated 

Write-Host "The folder "$Event.SourceEventArgs.FullPath" was $changeType at $timeStamp" -fore green 

Out-File -FilePath Z:log.txt -Append -InputObject "The folder $fpath was $changeType at $timeStamp" 

} 
1 Answers
Best Answer
AvatarAmit answered 4 months ago
Your Answer

18 + 12 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com