# Real-time file watcher to strip 0xFF from .pun files # Watches folder and cleans files as soon as they're created/modified $watchFolder = "C:\Dnc_Files\Q" # Change to your DNC program folder $fileFilter = "*.pun" # Watch .pun files Write-Host "Watching $watchFolder for new/modified $fileFilter files..." Write-Host "Press Ctrl+C to stop" # Create file system watcher $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $watchFolder $watcher.Filter = $fileFilter $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true # Define what to do when file is created or changed $action = { $path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType Write-Host "$(Get-Date -Format 'HH:mm:ss') - $changeType detected: $path" # Wait a moment for file to finish writing Start-Sleep -Milliseconds 500 try { # Read file as bytes $bytes = [System.IO.File]::ReadAllBytes($path) $originalCount = $bytes.Count # Remove all 0xFF bytes $cleaned = $bytes | Where-Object { $_ -ne 255 } $newCount = $cleaned.Count # Only rewrite if we found 0xFF if ($originalCount -ne $newCount) { [System.IO.File]::WriteAllBytes($path, $cleaned) $removed = $originalCount - $newCount Write-Host " [OK] Cleaned! Removed $removed byte(s) [0xFF]" -ForegroundColor Green } else { Write-Host " [SKIP] No 0xFF found, file OK" -ForegroundColor Gray } } catch { Write-Host " [ERROR] $_" -ForegroundColor Red } } # Register event handlers Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action # Keep script running try { while ($true) { Start-Sleep -Seconds 1 } } finally { # Cleanup on exit $watcher.Dispose() Write-Host "`nStopped watching folder" }