Skip to content

Instantly share code, notes, and snippets.

@Mimieam
Created February 13, 2013 04:13
Show Gist options
  • Save Mimieam/4942208 to your computer and use it in GitHub Desktop.
Save Mimieam/4942208 to your computer and use it in GitHub Desktop.
Sync my dropbox current project folder with my windows Github project folder ( very ugly because was done just to do the bare basic)
Unregister-Event FileDeleted
Unregister-Event FileCreated
Unregister-Event FileChanged
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$contextMenu = New-Object System.Windows.Forms.ContextMenu
$MenuItem = New-Object System.Windows.Forms.MenuItem
$objNotifyIcon.ContextMenu = $contextMenu
$objNotifyIcon.contextMenu.MenuItems.AddRange($MenuItem)
# $objNotifyIcon.contextMenu.MenuItems.Add($MenuItem)
$MenuItem.Text = "Exit"
# $MenuItem.add_Click({ Get-EventSubscriber | ? {$_.SourceObject -like "System.IO.FileSystemWatcher"} | Unregister-Event FileDeleted | Unregister-Event FileCreated | Unregister-Event FileChanged $objNotifyIcon.Visible = $false Write-Host "Please restart service ASAP" })
$MenuItem.add_Click({
Unregister-Event FileDeleted
Unregister-Event FileCreated
Unregister-Event FileChanged
$objNotifyIcon.Visible = $false
Write-Host "Please restart service ASAP"
})
$Source = 'C:\Users\LiliEAm\Dropbox\Projects\Scrabble' # Enter the root path you want to monitor.
$NewDestPath ='C:\Users\LiliEAm\Documents\GitHub\MScrab'
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories' to $true if required.
$watcher = New-Object IO.FileSystemWatcher $Source, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# Here, all three events are registerd. You need only subscribe to events that you need:
Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
$objNotifyIcon.Icon = "C:\Users\LiliEAm\Dropbox\Training\Google_Appengine_T\helloworld_unOptimized\Original\H9.ico"
$objNotifyIcon.BalloonTipIcon = "Info"
$objNotifyIcon.BalloonTipText = "The file '$name' was $changeType at $timeStamp"
$objNotifyIcon.BalloonTipTitle = "$changeType"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
# $objNotifyIcon.Dispose();
Out-File -FilePath outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
Copy-Item -Path $($Event.SourceEventArgs.FullPath) -Destination $NewDestPath
}
Unregister-Event FileDeleted
Register-ObjectEvent $watcher Deleted -SourceIdentifier FileDeleted -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore red
$objNotifyIcon.Icon = "C:\Users\LiliEAm\Dropbox\Training\Google_Appengine_T\helloworld_unOptimized\Original\H9.ico"
$objNotifyIcon.BalloonTipIcon = "Info"
$objNotifyIcon.BalloonTipText = "The file '$name' was $changeType at $timeStamp"
$objNotifyIcon.BalloonTipTitle = "$changeType"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
# $objNotifyIcon.Dispose();
Out-File -FilePath outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
$ItemToRemove = "$NewDestPath\$name"
Write-Host "Removing item : $ItemToRemove"
remove-item -Path $ItemToRemove
# Copy-Item -Path $($event.sourceEventArgs.FullPath) -Destination $NewDestPath
}
Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
$objNotifyIcon.Icon = "C:\Users\LiliEAm\Dropbox\Training\Google_Appengine_T\helloworld_unOptimized\Original\H9.ico"
$objNotifyIcon.BalloonTipIcon = "Info"
$objNotifyIcon.BalloonTipText = "The file '$name' was $changeType at $timeStamp"
$objNotifyIcon.BalloonTipTitle = "$changeType"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
# $objNotifyIcon.Dispose();
Out-File -FilePath outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
Copy-Item -Path $($event.sourceEventArgs.FullPath) -Destination $NewDestPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment