Skip to content

About

A Windows service that will run a powershell script or batch file if files change in a watched directory

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Run Script on Filesystem Changes

A Windows Service (built with .NET) that watches one or more directories for filesystem changes and runs a specified PowerShell or batch script for each watcher.

Useful for automating log processing, file ingestion, sync tasks, or any time you want scripts triggered by filesystem events.


Features

  • Watch multiple directories configured in a YAML file
  • Per-watcher toggle for IncludeSubdirectories
  • Runs PowerShell (.ps1) or Batch/EXE scripts
  • Global debouncing (one run per script after a configurable delay) to avoid bursts firing many runs
  • Simple installation via Install.bat / Uninstall.bat

Example config.yaml

debounceDelaySeconds: 120

watchers:
  - path: >
      C:\Users\pkste\Documents\Notes
    includeSubdirectories: true
    scriptPath: >
      C:\Users\pkste\Documents\Scripts\Test.bat

  - path: >
      C:\Watched\Reports
    includeSubdirectories: false
    scriptPath: >
      C:\Scripts\process-reports.bat

  - path: >
      D:\Data\Uploads
    includeSubdirectories: true
    scriptPath: >
      C:\Scripts\process-uploads.ps1

Fields

  • debounceDelaySeconds — global delay (in seconds) used for debouncing all watchers.
  • path — directory to monitor.
  • includeSubdirectories — true/false whether to watch subfolders.
  • scriptPath — absolute path to the script or executable to run when events are debounced.

Build

  1. Ensure .NET SDK (version used for the project) is installed.
  2. From the project root:
dotnet build
  1. The service executable will be under .\bin\Debug\net9.0\ (or netX.Y depending on project target).

Install (Install.bat)

Run the Install.bat as Administrator. The repository includes an Install.bat which contains the following commands:

copy .\config.yaml .\bin\Debug\net9.0\config.yaml
sc create RunScriptOnFilesystemChanges binPath= "~\source\RunScriptOnFilesystemChanges\RunScriptOnFilesystemChanges\bin\Debug\net9.0\RunScriptOnFilesystemChanges.exe"
sc start RunScriptOnFilesystemChanges

What it does:

  • Copies config.yaml into the output folder where the service executable runs.
  • Creates a Windows service named RunScriptOnFilesystemChanges.
  • Starts the service.

Important

  • You must run the Install.bat as Administrator.
  • Update the copy and sc create paths if your build output is in a different folder or you publish the app to another location.
  • sc create's binPath should point to the built .exe. If the path contains spaces, ensure proper quoting.

Uninstall (Uninstall.bat)

Run the Uninstall.bat as Administrator. The repository includes an Uninstall.bat which contains the following commands:

sc stop RunScriptOnFilesystemChanges
sc delete RunScriptOnFilesystemChanges

What it does:

  • Stops the service.
  • Deletes the service registration.

Logging & Troubleshooting

  • The service uses .NET logging. By default, logs will appear in the Windows Event Log under Application (check Event Viewer → Windows Logs → Application).

  • If scripts fail, the service logs standard output and standard error from the scripts.

  • Check the following if something doesn't run:

    • The service account has permission to access the watched directory and to execute the script.
    • The scriptPath is correct and executable.
    • PowerShell scripts are executed with -ExecutionPolicy Bypass by the service.
    • If you change config.yaml, restart the service to pick up changes.

Notes & Best Practices

  • Always test your scripts manually before relying on the service to invoke them.
  • If you need to deploy to production, consider publishing the service with dotnet publish and updating Install.bat to copy the published output.
  • Avoid long-running blocking operations inside the scripts that could overlap — if overlap is a concern, add locking inside your script or add logic in the service to check for running instances (not included by default).
  • Use absolute paths in config.yaml.

License

GPL-3.0

About

A Windows service that will run a powershell script or batch file if files change in a watched directory

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

⌂ Proxy