Delete files first before transferring new files

Advertisement

Ch33s3
Donor
Joined:
Posts:
5
Location:
World

Delete files first before transferring new files

Hi everyone,
I'm using a scripted batch automation to sync files from a local storage to a ftp location - it works like a charm since months! Thank you very much for your great tool and this great forum!
My Question would be if it is possible for WinSCP to first check remote files against local storage assets and delete obsolete remote files FIRST before transferring new files to the remote location?
This is my current setup, maybe you guys could help me out? ;
------
%winscp% /command ^
"option batch on" ^
"option confirm on" ^
"open %FTPTARGET%" ^
"synchronize remote -delete -mirror %destWF% %destDF%" ^
"exit" ^
/log=%destDL%\Sync_%datestamp%.log ^
/loglevel=-1
--------
Cheers, have a nice day! :)

Reply with quote

Advertisement

Ch33s3
Donor
Joined:
Posts:
5
Location:
World

thanks for the fast reply Martin!
alrighty then, maybe ill just call some PowerShell instance how you've described it.
Cheers, have a good day!

Reply with quote

Ch33s3
Donor
Joined:
Posts:
5
Location:
World

i finally took some time and assembled some code to work in PowerShell with .net assembly the usual sync task already works and is very fast, thanks!
currently the compare files section still doesnt work; in PowerShell ISE its telling me that
"[WinSCP.ComparisonDifference] no Method with name "Check" contained.

maybe someone is able to find the error (session options are stored seperately and work with sync)
# Compare Files
$comparisonDifferenceCollection = $session.CompareDirectories(
[WinSCP.SynchronizationMode]::Remote, "C:\Users\XXX\Desktop\WATCHFOLDER", "/DESTINATION", $True ,$False)
____
# Synchronize files #<---WORKING ALREADY!
# $synchronizationResult = $session.SynchronizeDirectories(
# [WinSCP.SynchronizationMode]::Remote, "C:\Users\Christoph\Desktop\WATCHFOLDER", "/DESTINATION", $True ,$True)

Greetings, have a great Weekend!

Last edited by Ch33s3 on 2019-12-01 22:52; edited 1 time in total

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
32,052
Location:
Prague, Czechia

I do not think you can get that error in the code you have posted - There's no Check call. The error must be from some other part of your code.

Reply with quote

Advertisement

Ch33s3
Donor
Joined:
Posts:
5
Location:
World

thanks!!! it seems that :

# Throw on any error
#$synchronizationResult.Check()
caused the error before, sorry im new to PowerShell but i like it already, like an "unlocked" version of WinSCP :)
now it seems to spool through that part without any error so far
i think im close, i just have to tell the script that it should check with local and if FileX is not present there, delete FileX on remote location..., (then proceed to the sync routine)
the return value ComparisonDifferenceCollection should probably be utilized somehow i assume?
Edit: here is my "frankensteined" build so far: https://pastebin.com/D9HtwWhJ
Cheers!

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
32,052
Location:
Prague, Czechia

$localPath = "C:\Users\XXXX\Desktop\WATCHFOLDER"
$remotePath = "/DESTINATION"
$diffs = $session.CompareDirectories(
    [WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath, $True)
foreach ($diff in $diffs)
{
    if ($diff.Action -eq [WinSCP.SynchronizationAction]::DeleteRemote)
    {
        Write-Host "Deleting $($diff.Remote.FileName)..."
        $session.RemoveFiles(
            [WinSCP.RemotePath]::EscapeFileMask($diff.Remote.FileName)).Check()
    }
}
 
foreach ($diff in $diffs)
{
    if (($diff.Action -eq [WinSCP.SynchronizationAction]::UploadNew) -or
        ($diff.Action -eq [WinSCP.SynchronizationAction]::UploadUpdate))
    {
        Write-Host "Uploading $($diff.Local.FileName)..."
        $session.PutFiles(
            $diff.Local.FileName, $remotePath + "/*", $False).Check()
    }
}

Reply with quote

Ch33s3
Donor
Joined:
Posts:
5
Location:
World

Aaah, thats it!
For testing, I just quickly merged the deleting part and it works like a charm!
Cheers, have a good day and thanks for all your help !!!

Reply with quote

Advertisement

You can post new topics in this forum