Hello,
I’m trying to make a remote file sync process as safe as possible (WinSCP, Powershell). I want to safeguard against downloading remote files which aren’t quite written to yet.
I’ll post the script, but first some caveats:
- Powershell as a language is the preference of my management (as is WinSCP)
- Can’t be sure on file names or extensions so I’m using “Sync and delete” instead of “Copy and delete”
- The flow of files into the remote dir is unpredictable
Preferences aside, please please feel free to suggest whatever might make me think outside the box to satisfy the requirement.
Code follows:
$synchronizationResult = $session.SynchronizeDirectories(
\[WinSCP.SynchronizationMode\]::Local, $localOrdersPath, $remoteOrdersPath, $False)
foreach ($download in $synchronizationResult.Downloads) {
\# Success or error?
if ($download.Error -eq $Null)
{
$msg = "Downloaded Order: $($order.FileName) at $(Get-Date), removing from source"
$transferLog += $msg
\# Download succeeded, remove file from source
$filename = \[WinSCP.RemotePath\]::EscapeFileMask($download.FileName)
$removalResult = $session.RemoveFiles($filename)
if ($removalResult.IsSuccess)
{
$msg = "Removing of file $($download.FileName) succeeded"
$transferLog += $msg
}
else
{
$msg = "Removing of file $($download.FileName) failed"
$transferLog += $msg
}
}
else
{
$msg = "Download of $($download.FileName) failed: $($download.Error.Message)"
$transferLog += $msg
}
}
As this is a code snippet, please assume all variables are assigned. Current testing of the protocol has been successful but I’m looking to jazz it up a little. (I’ll upload the full if needed)
PS
I’ve seen a solution here: Locking files while uploading / Upload to temporary file name :: WinSCP. But these solutions aren’t applied to sync and I’m not sure how I would structure the commands.
All the best