FuSiOn FuSiOn - 1 year ago 275
PowerShell Question

FileSystemWatcher detect when file is moved to folder

I'm having trouble with a script that's monitoring a folder.
FileSystemWatcher only seems to detect when a file is being copied to the folder, not when it's just being moved to it from the same drive.
Is there a way to detect this?

$desFolder = "H:\Media"
$ExFolder = "H:\Monitor"

$Filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $ExFolder, $Filter
$fsw.IncludeSubdirectories = $true

$fswOnChange = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier FileUpdated -Action {
$File = Get-Item $EventArgs.FullPath
if($File.Name -imatch '\.(?:mp4|mkv)$' -and $file.Name -match '.*?S\d+E+'){
Start-Sleep -s 1
if(Test-FileReady $File.FullName){
Move-Files $File
function global:Test-FileReady {
if (Test-Path -LiteralPath $Path) {
trap {
return $false
$stream = New-Object system.IO.StreamReader $Path
if ($stream) {
return $true
function global:Move-Files{
Write-Host $File.Name

Answer Source

Try using Renamed and Created events as well.

BTW, the IO.FileSystemWatcher docs say:

The component will not watch the specified directory until the Path is set, and EnableRaisingEvents is true

$fsw.EnableRaisingEvents = $true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download