Forum Discussion
Turion986
Aug 31, 2023Copper Contributor
Help with Watcher
Hey guys, some of you can help me to check what is wrong with this script? thank you in advance! # Cartella da monitorare (stessa cartella) $cartellaDaMonitorare = "C:\TEST" # Crea un re...
- Sep 22, 2023
Turion986 Ok... I found another script example and rewrote it again for you... I had some issues with files being renamed repeatedly and added renamed to the filename to exclude that...
#https://dotnet-helpers.com/powershell/how-to-monitor-a-folder-changes-using-powershell/ ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO $filewatcher = New-Object System.IO.FileSystemWatcher #Mention the folder to monitor $filewatcher.Path = "C:\Test" $filewatcher.Filter = "*.msg" $log = "C:\Test\FileWatcher_log.txt" #include subdirectories $true/$false $filewatcher.IncludeSubdirectories = $true $filewatcher.EnableRaisingEvents = $true ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED $writeaction = { $path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType if (-not ($path -match 'renamed')) { $now = Get-Date -Format "yyyyMMddHHmm" $filename = [System.IO.Path]::GetFileNameWithoutExtension($path) $extension = [System.IO.Path]::GetExtension($path) $newname = "${now}_renamed_${filename}${extension}" Rename-Item -Path $path -NewName $newname $logline = "$(Get-Date), $changeType, $path, $newname" Add-content $log -value $logline } } ### DECIDE WHICH EVENTS SHOULD BE WATCHED #The Register-ObjectEvent cmdlet subscribes to events that are generated by .NET objects on the local computer or on a remote computer. #When the subscribed event is raised, it is added to the event queue in your session. To get events in the event queue, use the Get-Event cmdlet. Register-ObjectEvent $filewatcher "Created" -Action $writeaction Register-ObjectEvent $filewatcher "Changed" -Action $writeaction Register-ObjectEvent $filewatcher "Renamed" -Action $writeaction while ($true) { Write-Host ("Monitoring folder {0}" -f $filewatcher.Path) Start-Sleep 5 }
Logfile:
09/22/2023 21:00:45, Renamed, C:\Test\test1.msg, 202309222100_renamed_test1.msg
09/22/2023 21:00:45, Renamed, C:\Test\test1.msg, 202309222100_renamed_test1.msg
09/22/2023 21:01:00, Renamed, C:\Test\test2.msg, 202309222101_renamed_test2.msg
09/22/2023 21:01:00, Renamed, C:\Test\test2.msg, 202309222101_renamed_test2.msg
09/22/2023 21:01:20, Renamed, C:\Test\Testfolder\test3.msg, 202309222101_renamed_test3.msg
09/22/2023 21:01:20, Renamed, C:\Test\Testfolder\test3.msg, 202309222101_renamed_test3.msg
09/22/2023 21:01:41, Renamed, C:\Test\Testfolder\Testsubfolder\test5.msg, 202309222101_renamed_test5.msg
09/22/2023 21:01:41, Renamed, C:\Test\Testfolder\Testsubfolder\test5.msg, 202309222101_renamed_test5.msg09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
Logging is doubled, tripled per file... I'm not sure why that is happening... Is this something you can work with?
Turion986
Sep 13, 2023Copper Contributor
maybe.....this is over my programming knowledge.......do you have suggestions for where and what to look/search for? Even if i search other solutions, i don´t want to implement 3rd part software on our fileserver.....
Sep 22, 2023
Turion986 Ok... I found another script example and rewrote it again for you... I had some issues with files being renamed repeatedly and added renamed to the filename to exclude that...
#https://dotnet-helpers.com/powershell/how-to-monitor-a-folder-changes-using-powershell/
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$filewatcher = New-Object System.IO.FileSystemWatcher
#Mention the folder to monitor
$filewatcher.Path = "C:\Test"
$filewatcher.Filter = "*.msg"
$log = "C:\Test\FileWatcher_log.txt"
#include subdirectories $true/$false
$filewatcher.IncludeSubdirectories = $true
$filewatcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$writeaction = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
if (-not ($path -match 'renamed')) {
$now = Get-Date -Format "yyyyMMddHHmm"
$filename = [System.IO.Path]::GetFileNameWithoutExtension($path)
$extension = [System.IO.Path]::GetExtension($path)
$newname = "${now}_renamed_${filename}${extension}"
Rename-Item -Path $path -NewName $newname
$logline = "$(Get-Date), $changeType, $path, $newname"
Add-content $log -value $logline
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
#The Register-ObjectEvent cmdlet subscribes to events that are generated by .NET objects on the local computer or on a remote computer.
#When the subscribed event is raised, it is added to the event queue in your session. To get events in the event queue, use the Get-Event cmdlet.
Register-ObjectEvent $filewatcher "Created" -Action $writeaction
Register-ObjectEvent $filewatcher "Changed" -Action $writeaction
Register-ObjectEvent $filewatcher "Renamed" -Action $writeaction
while ($true) {
Write-Host ("Monitoring folder {0}" -f $filewatcher.Path)
Start-Sleep 5
}
Logfile:
09/22/2023 21:00:45, Renamed, C:\Test\test1.msg, 202309222100_renamed_test1.msg
09/22/2023 21:00:45, Renamed, C:\Test\test1.msg, 202309222100_renamed_test1.msg
09/22/2023 21:01:00, Renamed, C:\Test\test2.msg, 202309222101_renamed_test2.msg
09/22/2023 21:01:00, Renamed, C:\Test\test2.msg, 202309222101_renamed_test2.msg
09/22/2023 21:01:20, Renamed, C:\Test\Testfolder\test3.msg, 202309222101_renamed_test3.msg
09/22/2023 21:01:20, Renamed, C:\Test\Testfolder\test3.msg, 202309222101_renamed_test3.msg
09/22/2023 21:01:41, Renamed, C:\Test\Testfolder\Testsubfolder\test5.msg, 202309222101_renamed_test5.msg
09/22/2023 21:01:41, Renamed, C:\Test\Testfolder\Testsubfolder\test5.msg, 202309222101_renamed_test5.msg
09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
09/22/2023 21:03:08, Created, C:\Test\test6.msg, 202309222103_renamed_test6.msg
Logging is doubled, tripled per file... I'm not sure why that is happening... Is this something you can work with?
- Sep 28, 2023That seems to work, and this would skip files having rename it it and does only skip files with _renamed_ in the filename (I just tested it)
- Turion986Sep 28, 2023Copper Contributor
Harm_Veenstra only to let you know...
maybe.....i found a little workaround..... i change the operator from match to like
$path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType if (-not ($path -like '*_renamed_*')) {
do you see any problems with this solution that i am not aware of?
- Sep 28, 2023Yes, that's the downside... If you have a filename with "rename" it will skip that. Please mark my answer with the rewritten scripts as Best Response & Like to mark this topic as solved
- Turion986Sep 28, 2023Copper Contributor
Harm_Veenstra this is working very fine and at the moment is stable....also the Log File is clear, not doubled as on your side, but in any case i will take a look again when the log will be more full...
there is only a little problem that i found during the test.....is the name of the TAG......
basically if i will create a new test file "renamed_contract.msg" it is obviuosly skipped.
i will try to find a workaround to avoid it.....