A
A
Andrew2012-01-28 12:38:45
FTP
Andrew, 2012-01-28 12:38:45

Auto-upload of a file via FTP on its change

Are there programs that allow you to monitor files and automatically upload them to the server if they have changed? Everything I've found on the internet doesn't do it properly.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
YourChief, 2012-01-28
@YourChief

On Linux, there is inotify, which can be used to implement this in a couple of lines. and, it seems, there are interfaces to this either, in the form of system utilities. on Windows, probably, these are some kind of hooks

@
@ntkt, 2012-01-28
_

On PowerShell (default on Win 7), it's something like this ( scrape in the bottom of the barrel and stupidly in the forehead , so it's better to test :) ):
We use two dotnet classes - System.IO.FileSystemWatcher and System.Net.WebClient.
Subscribe to acc. FS events, we get the file name from the event details and upload it using WebClient. The WebClient is smart and can be easily converted to SFTP later.

#
# Watch for files in $watchDir that a match a filer $watchFilter
#    and upload them to FTP $ftpUrl when they are changed (modified)
#

$ftpUrl = "ftp://username:[email protected]/pub/incoming/"
$watchDir = "b:\temp"
$watchFilter = "*.txt"

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 5
}

# EOF

V
Vladislav Klimanov, 2012-01-28
@ahmpro

nnCron + scripts (cmd/python/autoit/...) + console ftp client

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question