Skip to content

Instantly share code, notes, and snippets.

@ravenstar
Created June 13, 2018 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravenstar/6ab143581d20fd569c1c08a6858c68cd to your computer and use it in GitHub Desktop.
Save ravenstar/6ab143581d20fd569c1c08a6858c68cd to your computer and use it in GitHub Desktop.
#
# Watches for changes to files matching the $watchFilter within the $watchDir
# directory, then uploads them to a FTP server.
# It is recommended to use a scheduled task to ensure this script runs on
# system boot.
#
## Config - Change these.
$ftpUrl = "ftp://username:password@example.com/some/folder/"
$watchDir = "C:\temp\"
$watchFilter = "*.txt"
## Do Not Edit Below
function uploadFile($fullFileName)
{
$webclient = New-Object System.Net.WebClient
$fileName = [system.io.path]::GetFileName($fullFileName)
$fileUrl = $ftpUrl+$fileName
$uri = New-Object System.Uri($fileUrl)
try
{
$rc = $webclient.UploadFile($uri, $fullFileName)
}
catch [System.Net.WebException]
{
Write-Host "[ERR]: "$_
return
}
Write-Host "Uploaded $fullFileName"
}
$watcher = New-object System.IO.FileSystemWatcher $watchDir
$watcher.EnableRaisingEvents = $true
$watcher.Filter = $watchFilter
$changed = Register-ObjectEvent $watcher "Changed" -Action {
Write-Host $eventArgs.ChangeType, $eventArgs.Fullpath
uploadFile $eventArgs.Fullpath
}
while($true) {
echo "."
start-sleep -s 10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment