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.
Advertisement
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
- Checking for a separate file for each uploaded file
- Further reading
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"
Advertisement
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 $remoteFilePath ..." # Download and delete $session.GetFiles( [WinSCP.RemotePath]::EscapeFileMask($remoteFilePath), $localPath + "\*", $True).Check() # Delete ".done" file $session.RemoveFiles([WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)).Check() }
Further reading
- Guide to scripting/automation;
- WinSCP .NET assembly;
- Using WinSCP .NET assembly from PowerShell;
- Locking files while uploading / Upload to temporary file name;
- Checking file existence;
- Conditional processing in automation.