On Create Event - move and rename File


thanks to kazun, have script below works. 

the folder structure test incoming...then 2 subfolders - folder1 , folder2

when file created in folder1 need moved test incoming directoryname (folder1) appended beginning of existing file name.

ex.  xxxx.tif created in folder1.  need moved test incoming name folder1xxxx.tif. 

i can't seem work correctly.  can tell me how can know correct variable use create this?  here code without tests in it..

 

 

param ([string]$folder = 'c:\cpr7\faxes\test incoming',  $icon = "c:\batch files\monitoring.ico",  $destpath = 'c:\cpr7\faxes\test incoming\'  )   [reflection.assembly]::loadwithpartialname("system.windows.forms") | out-null   $form = new-object system.windows.forms.form  $ni = new-object system.windows.forms.notifyicon  $nimenu = new-object system.windows.forms.contextmenu  $watcher = new-object system.io.filesystemwatcher $folder   $ni.icon = new-object system.drawing.icon($icon)  $ni.contextmenu = $nimenu   $ni.text = "watching $folder.."   $miexit = new-object system.windows.forms.menuitem  $miexit.text = "exit"  $miexit.add_click({ $ni.visible = $false  $form.close() })   $nimenu.menuitems.addrange(@($miexit))   $watcher.filter = "*.tif"  $watcher.includesubdirectories = $true  $watcher.synchronizingobject = $form  $form.showintaskbar = $false  $form.windowstate = "minimized"    $watcher.notifyfilter = [system.io.notifyfilters]::lastaccess , [system.io.notifyfilters]::lastwrite, [system.io.notifyfilters]::filename,[system.io.notifyfilters]::directoryname  #$watcher.add_changed({  #$message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"  #$ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"warning")  #write-host $message  #out-file -filepath c:\batch files\filemonitor.log -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."  #})  $watcher.add_created({  $message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"  $ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")  write-host $message  out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."  move-item $($_.fullpath) -destination $destpath  write-host $($_.fullpath) ($destpath+$destfile)  })  $watcher.add_deleted({  $message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"  $ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")  write-host $message  out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."  })  $watcher.add_renamed({  $message = "$((get-date).toshorttimestring()) : $($_.oldfullpath) renamed $($_.fullpath) $($_.changetype)"  $ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")  out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.oldfullpath) $($_.changetype) $($_.fullpath)."  write-host $message  })   $watcher.enableraisingevents = $true  $ni.visible = $true  $form.showdialog() 

 



i figured part out...here complete code.  added these 2 lines move event.

 

$destfile = $($_.name).tostring().replace("\","")
move-item $($_.fullpath) -destination $destpath$destfile

here complete code....

param ([string]$folder = 'e:\cpr7\faxes\test incoming',
$icon = "c:\batch files\monitoring.ico",
$destpath = 'e:\cpr7\faxes\test incoming\'
)

[reflection.assembly]::loadwithpartialname("system.windows.forms") | out-null

$form = new-object system.windows.forms.form
$ni = new-object system.windows.forms.notifyicon
$nimenu = new-object system.windows.forms.contextmenu
$watcher = new-object system.io.filesystemwatcher $folder

$ni.icon = new-object system.drawing.icon($icon)
$ni.contextmenu = $nimenu 
$ni.text = "watching $folder.."

$miexit = new-object system.windows.forms.menuitem
$miexit.text = "exit"
$miexit.add_click({ $ni.visible = $false
$form.close() })

$nimenu.menuitems.addrange(@($miexit))

$watcher.filter = "*.tif"
$watcher.includesubdirectories = $true
$watcher.synchronizingobject = $form
$form.showintaskbar = $false
$form.windowstate = "minimized"


$watcher.notifyfilter = [system.io.notifyfilters]::lastaccess , [system.io.notifyfilters]::lastwrite, [system.io.notifyfilters]::filename,[system.io.notifyfilters]::directoryname
#$watcher.add_changed({
#$message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"
#$ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"warning")
#write-host $message
#out-file -filepath c:\batch files\filemonitor.log -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."
#})
$watcher.add_created({
$message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"
$ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")
write-host $message
out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."
$destfile = $($_.name).tostring().replace("\","")
move-item $($_.fullpath) -destination $destpath$destfile
})
$watcher.add_deleted({
$message = "$((get-date).toshorttimestring()) : $($_.fullpath) $($_.changetype)"
$ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")
write-host $message
out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.fullpath) $($_.changetype)."
})
$watcher.add_renamed({
$message = "$((get-date).toshorttimestring()) : $($_.oldfullpath) renamed $($_.fullpath) $($_.changetype)"
$ni.showballoontip(10,$_.fullpath,$message,[system.windows.forms.tooltipicon]"info")
out-file -filepath "c:\batch files\filemonitor.log" -append -inputobject "$(get-date -f g) : file $($_.oldfullpath) $($_.changetype) $($_.fullpath)."
write-host $message
})

$watcher.enableraisingevents = $true
$ni.visible = $true
$form.showdialog()



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607