Topic "Verify Checksum before deleting files ..."

Author Message
othmane
[View user's profile]

Joined: 2016-04-01
Posts: 4
Location: Paris
Hello,

I found 2 scripts in the powershell examples page , I am using the first to download and delete files
the second script compare local file with remote file checksum

my objectif is to verify checksum before deleting files from remote directories.

any idea about how to include the checksum verification inside the first script ?

script 1 :

fore more details (I replaced synchronize with get function) : https://winscp.net/eng/docs/library_example_delete_after_successful_download

Code:
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        FtpSecure = [WinSCP.FtpSecure]::Implicit
        HostName = "xxxx"
        UserName = "xxxx"
        Password = "xxxxx"
        PortNumber = "990"
        FtpMode = "Passive"
        TlsHostCertificateFingerprint = "xxxxxx"
    }
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "x:\xxxx.log"
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.FileMask = "*"
        $transferOptions.PreserveTimestamp = $true
 
        $transferResult = $session.GetFiles("/","x:\xxxx", $False, $transferOptions)   
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
           #$removalResult = $session.RemoveFiles($session.EscapeFileMask($transfer.FileName))
            #if ($removalResult.IsSuccess)
                #{
                    #Write-Host ("Removing of file {0} succeeded" -f
                        #$transfer.FileName)
                #}
                #else
                #{
                    #Write-Host ("Removing of file {0} failed" -f
                        #$transfer.FileName)
                #}
        }

    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}


script 2 :

fore more details : https://winscp.net/eng/docs/library_example_verify_file_checksum

Code:
param (
    # Use Generate URL function to obtain a value for -sessionUrl parameter.
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
    [Parameter(Mandatory)]
    $localPath,
    [Parameter(Mandatory)]
    $remotePath,
    [Switch]
    $pause = $False
)
 
try
{
    Write-Host $localPath
 
    # Calculate local file checksum
    $sha1 = [System.Security.Cryptography.SHA1]::Create()
    $localStream = [System.IO.File]::OpenRead($localPath)
    $localChecksum = [System.BitConverter]::ToString($sha1.ComputeHash($localStream))
 
    Write-Host $localChecksum
   
    # Load WinSCP .NET assembly
    $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
    Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.ParseUrl($sessionUrl)
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
       
        Write-Host $remotePath
 
        # Calculate remote file checksum
        $remoteChecksum = [System.BitConverter]::ToString($session.CalculateFileChecksum("sha-1", $remotePath))
        Write-Host $remoteChecksum
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    # Compare cheksums
    if ($localChecksum -eq $remoteChecksum)
    {
        Write-Host "Match"
        $result = 0
    }
    else
    {
        Write-Host "Does NOT match"
        $result = 1
    }
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    $result = 1
}
 
# Pause if -pause switch was used
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}
 
exit $result
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
So just move the CalculateFileChecksum and ComputeHash calls before the RemoveFiles call. And compare the results before actually calling the RemoveFiles.
othmane
[View user's profile]

Joined: 2016-04-01
Posts: 4
Location: Paris
OK,
thank you
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License