Downloading files from FTP/SFTP server only after "done" file is created

A common problem with a regular download of updates from FTP/SFTP server, is to recognize, when files are ready to be download. I.e. how to recognize, when the files are finished uploading.

As discussed in locking files while uploading/upload to temporary file name article, one of possible solutions is, that once the files are ready, a special “done” file is uploaded to indicate that.

The “done” file can either be a file with a fixed name, like done, indicating readiness of all files (in a folder). Or there can be a separate file for each uploaded file, with the same name as the actual file except for some special extension/suffix, e.g. .done.

Checking for a file with a fixed name

Using WinSCP scripting

Checking for a file with a fixed name is an easy task, for which you can use a simple scripting.

The following batch file checks for an existence of done file. If the file exists, it downloads and deletes all files from the remote folder.

@echo off
 
winscp.com /command ^
    "open mysession" ^
    "stat /remote/path/done" ^
    "get -delete /remote/path/* C:\local\path\*" ^
    "exit"

See also the article about checking file existence.

Using WinSCP .NET Assembly

For an example, see the article about conditional processing in automation.

Checking for a separate file for each uploaded file

An example snippet of PowerShell script that lists all files with .done extension, and downloads and deletes corresponding files.

$remotePath = "/remote/path"
$localPath = "C:\local\path"
 
$files = $session.EnumerateRemoteFiles($remotePath, "*.done", [WinSCP.EnumerationOptions]::None)
foreach ($fileInfo in $files)
{
    # Resolve actual file name by removing the .done extension
    $remoteFilePath = $fileInfo.FullName -replace ".done$", ""
    Write-Host ("Downloading {0} ..." -f $remoteFilePath)
    # Download and delete
    $session.GetFiles($session.EscapeFileMask($remoteFilePath), $localPath + "\*", $True).Check() 
    # Delete ".done" file
    $session.RemoveFiles($session.EscapeFileMask($fileInfo.FullName)).Check()
}

Further reading

 
  script_downloading_when_done_file_exists.txt · Last modified: by martin
 

Search

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate