Size Limit for Script to Recursively Monitor Folder for Changes

Hey Guys -

I’m an avid user of Plex which hosts / streams local media to many of my devices. Unfortunately, it’s internal mechanism for updating its libraries isn’t too robust. It has an option to trigger library updates when changes are detected, however; my media resides on a mapped drive therefore does not work. As it includes a CLI utility which can trigger library updates using parameters, I thought I’d just write a solution myself.

At the end of this post is the test script I started which should recursively monitor a folder for new .mp4 files. Upon finding one, it sets it to $path then does the following:

  • Determines the library to update based on the path
  • Executes the update
  • Logs to a file
  • Displays a notification in Windows

Before proceeding, I wanted to ask a couple of questions, please…

  1. My Movies path has almost 5,500 folders - each with ~25 files - a lot to monitor. Would the monitoring script I have planned be efficient enough and/or use too many resources if so?
  2. If acceptable or if a better solution is suggested, what would be the best way to have it monitor multiple paths and specific file extensions?

Test Script

# Monitor Path for New RAR Files
$folder = "D:\- Media -\Movies"
$filter = "*.mp4"
$Watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier MovieMonitor -Action {
$path = $Event.SourceEventArgs.FullPath

$plexdatafolderroot = “D:\Plex Media Server”
$database = “$plexdatafolderroot\Plug-in Support\Databases\com.plexapp.plugins.library.db”
$plexscanner = “C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Scanner.exe”
$logfile = “D:\App Logs\Sonarr\Script Import\Sonarr-PlexScan.log”
$tvmain = “*TV Shows*”
$moviemain = “*Movies*”

#Determine Library to Update
If ($path -like $tvmain ) { $libraryid = 2 }
If ($path -like $moviemain ) { $libraryid = 6 }

#Execute Library Scan for Show Path
& “$plexscanner” --scan --refresh --section $libraryid --directory $path

#Write to Log File
write-output “$path” “$libraryid” | add-content $logfile

#Display Notification
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = “Plex has started scanning $path!”
$balloon.BalloonTipTitle = “Plex Scan Triggered!”
$balloon.Visible = $true

It’s hard to say what the best solution is without testing. The watcher approach is going to be sitting in memory until your restart it or it runs out of resources. Another approach could be just running a scheduled task that does a Get-ChildItem on the parent directory and then looking a creation or modification time on the files. It’s a triggered approach versus a scheduled scan. It would require testing to see the pros\cons of the approaches, but the watchers are difficult to know if they are watching where as a scan you know is running every X number of minutes. My .02