Differences between PowerShell ISE and Powershell.exe when using IO.FileSystemWatcher -
i have following powershell script running fine in ise, mean runs , waits files created before processing them. problem try run command line exits @ $oncreated = register-objectevent $fsw created -sourceidentifier filecreated -action
call.
how can run commandline indefinitely e.g. waiting , processing each new file meets criteria in target folder?
i must missing something, first attempt @ using powershell advice on how working cmd line appreciated. if have suggestions on improvements i'd love hear them too.
#variables $folder = 'c:\de\live' $filter = 'lock.txt' $data_in = 'c:\de\live\data.txt' $data_out = 'c:\de\live\rmin\data.txt' $destination = 'c:\de\live\rmin\lock.txt' #setup file system watcher on live dir watching file name , last write $fsw = new-object io.filesystemwatcher $folder, $filter -property @{ includesubdirectories = $false notifyfilter = [io.notifyfilters]'filename, lastwrite' } write-host "----------------------------------------------------" write-host "monitoring: '$folder'" $oncreated = register-objectevent $fsw created -sourceidentifier filecreated -action { $path = $event.sourceeventargs.fullpath if(test-path ($data_in)){ write-host "processing: data.txt" try { get-content $data_in | foreach-object { $output = $output + $_.trim() + "|" } $output = $output + "1" } catch { $error[0] unregister-event -sourceidentifier filecreated write-host "error ocurred, monitoring stopped" write-host "----------------------------------------------------" } { write-host "processing: data.txt finished" } write-host "writing: data.txt rmin" try{ $stream = [system.io.streamwriter] $data_out $stream.writeline("add dave") $stream.writeline($output) $stream.close() } catch{ $error[0] unregister-event -sourceidentifier filecreated write-host "error ocurred, monitoring stopped" write-host "----------------------------------------------------" } { write-host "writing: data.txt rmin finished" } write-host "moving: lock.txt file rmin" try{ move-item $path -destination $destination -force -verbose } catch{ $error[0] unregister-event -sourceidentifier filecreated write-host "error ocurred, monitoring stopped" write-host "----------------------------------------------------" } finally{ write-host "moving: lock.txt file rmin finished" } write-host "deleting: data.txt" try{ remove-item $data_in } catch{ $error[0] unregister-event -sourceidentifier filecreated write-host "error ocurred, monitoring stopped" write-host "----------------------------------------------------" } finally{ write-host "deleting: data.txt finished" write-host "----------------------------------------------------" write-host "awaiting next file..." } } }
Comments
Post a Comment