How to check file existence on local and remote path, then stop script if it does not exist

Advertisement

vmsystems
Guest

How to check file existence on local and remote path, then stop script if it does not exist

Script Type: PowerShell
Use case: For SFTP Uploads and Downloads

Hello WinSCP Team

I am using PowerShell + WinSCPnet.dll to automate a file uploads and downloads.

Problem description Upload Script:
At the start the script should check if files are present on the localpath, if yes the script should be executed to the end and if no the script should be aborted.

Problem description Download Script:
After the script has successfully established the session via SFTP to the server, the script should first check if files are available on this server or not, if the files are not available the script should be terminated and if files are available the script should be executed until the end.

Unfortunately, I have not found a simpler description in the forum how to perform a file check on a localpath and externalpath. I would like to extend this script with this function.

It is not a specific file type, but is simply to check "*" everything in the path.

Could anybody help me with this?

Code Upload Script:
Start-Transcript %LOCALPATH%\Powershell.log
 
# Load WinSCP .NET assembly
Add-Type -Path "%LOCALPATH%\WinSCPnet.dll"
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "%SERVER%"
    UserName = "%USER%"
    PrivateKeyPassphrase = "%PASSWORD PRIVATE KEY%"
    SshPrivateKeyPath = "%PRIVATE KEY%"
    SshHostKeyFingerprint = "ssh-rsa 4096"
}
 
$session = New-Object WinSCP.Session
$session.SessionLogPath = "%LOCALPATH%\WinSCP.log"
 
try
{
    # Connect
    $session.Open($sessionOptions)
    
    # Upload files & Remove original
    $session.PutFiles("%LOCALPATH%\*", "%REMOTEPATH%\", $true).Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}  
 
Stop-Transcript
Code Download Script:
Start-Transcript %LOCALPATH%\Powershell.log
 
# Load WinSCP .NET assembly
Add-Type -Path "%LOCALPATH%\WinSCPnet.dll"
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "%SERVER%"
    UserName = "%USER%"
    PrivateKeyPassphrase = "%PASSWORD PRIVATE KEY%"
    SshPrivateKeyPath = "%PRIVATE KEY%"
    SshHostKeyFingerprint = "ssh-rsa 4096"
}
 
$session = New-Object WinSCP.Session
$session.SessionLogPath = "%LOCALPATH%\WinSCP.log"
 
try
{
     # Connect
     $session.Open($sessionOptions)
 
     # Download files
     $session.GetFiles("/%REMOTEPATH%/*", "%LOCALPATH%\*").Check()
 
     # Remove files
     $session.RemoveFiles("/%REMOTEPATH%/*")
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}  
 
Stop-Transcript

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,378
Location:
Prague, Czechia

Re: How to check file existence on local and remote path, then stop script if it does not exist

vmsystems wrote:

Problem description Upload Script:
At the start the script should check if files are present on the localpath, if yes the script should be executed to the end and if no the script should be aborted.
Checking for an existence of local files in not a WinSCP question.
Anyway, you can use Test-Path:
https://winscp.net/eng/docs/script_checking_file_existence#local

Problem description Download Script:
After the script has successfully established the session via SFTP to the server, the script should first check if files are available on this server or not, if the files are not available the script should be terminated and if files are available the script should be executed until the end.
That's exactly what your script does now. It does not need any change.
Anyway, again, see:
https://winscp.net/eng/docs/script_checking_file_existence#library

Though note that you should not use Session.GetFiles followed by Session.RemoveFiles. That's not transactionally safe. You might end up deleting files that were not downloaded. Instead use remove argument of Session.GetFiles. The same way you do with Session.PutFiles in your upload script.
$session.GetFiles("/%REMOTEPATH%/*", "%LOCALPATH%\*", $True).Check()

Reply with quote

Advertisement

You can post new topics in this forum