I have a script (.ps1
) that is monitoring a directory, and when a file is created it should open that specific file (in this case a .pdf
).
There are multiple files in the directory, but it should only open the one that gets detected as it gets created/moved to that directory when the script is running.
A new file will be continuously added so the name changes slightly every time. Example1.pdf, Example2.pdf and so on. This means that i need to open the most recent file, regardless of the name.
Right now it is only monitoring the directory, because i am not sure how to proceed with opening the file when it gets detected.
I would like to keep everything in this single script if possible.
(I am not that experienced with powershell. Howerver, I am experienced with cmd/batch. I am trying to convert to powershell)
Any good methods to make this work?
This is my script:
$host.UI.RawUI.WindowTitle = "Watcher"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\top_secret_path\test"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $false
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
Add-content "C:\top_secret_path\log.txt" -value $logline
}
Register-ObjectEvent $watcher "Created" -Action $action | Format-Table -AutoSize
while ($true) {sleep 5}
No comments:
Post a Comment