Author Posts

July 24, 2018 at 8:07 am

Hi all,

here i go again, one more time I'm trying to get into Powershell programming. I have a project between hands where programming is involved. Since i'm a sysadmin and not a programmer, i'm struggling with this, but I don't wanna give up. I've tried many times to learn PS from scratch but then i'm not using it in my daily tasks and it's always fading away.
Now I wanna use this project to push forward and stablish my knoledge. I'm adapting other scripts for my purpose.
I'd really appreciate if someone can help me with that.
I'm building an app were i'm controlling Photo cameras over USB relays, then the cameras copy the Jpgs in a Temp folder, from there they should be moved to another folder.
With FileSystemWatcher i wanna know when the last picture is transferred to the temp folder in order to continue with process. I put this lines together, but is not working.

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "c:\temp"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$nrfiles = 0

Register-ObjectEvent $watcher "Created" -Action {$nrfiles++}


do {
     Write-Host "." -NoNewline
    } until ($nrfiles -eq "3")

The value of $nrfiles doesn't increase 🙁

For any help, thanx in advance.

July 24, 2018 at 8:55 am

try changing

Register-ObjectEvent $watcher "Created" -Action {$nrfiles++}

to

Register-ObjectEvent -inputObject $watcher -eventname Created -Action {$nrfiles++}

July 24, 2018 at 9:08 am

Hi Simon B,

still doesn't increase. No idea where can be the problem.

Thanks anyway for the quick reply.

Greetings

July 24, 2018 at 10:04 am

Not sure if this will help you but this will move created txt files from one directory to another

$Source = 'C:\Temp'
$Filter = '*.txt'                             # < -- set this according to your requirements
$destination = 'c:\Test'
$Watcher= New-Object IO.FileSystemWatcher $Source, $filter -Property @{
 IncludeSubdirectories = $true              # <-- set this according to your requirements
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
$Path = $Event.SourceEventArgs.FullPath
Move-Item $Path -Destination $destination -verbose

}

July 25, 2018 at 2:49 pm

Hi Simon B,

thank you very much for the script!, i was able to adapt it to my purpose.

Greetings