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:
Code Download Script:
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
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