1

In an SSIS package using the execute process task I am executing a script file which is WinSCP scripting to connect to an FTP site via WinSCP.

I am able to use the basics like get, put, open commands in WinSCP.

But there is a scenario where I want to check if a file exist on an FTP site and if it does I pull the file else I pull a different file WinSCP.

However it seems that the moment I put if it just fails the script like it cannot read that syntax.

Does if statement not exist for WinSCP script files?

I was using this link for reference in building the if statement: https://winscp.net/eng/docs/script_checking_file_existence#scripting

This is the WinSCP script file code I have.

# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect
open FTP
# Remote FTP Folder
cd SSISFTP
# get file attributes
stat FILEONE.txt
# conditional statement
if %ERRORLEVEL% neq 0 goto error
# Get FILEONE File
get FILEONE.txt
# Disconnect
close
# Exit WinSCP
exit /b 0

:error
get FILETWO.txt
# Disconnect
close
# Exit WinSCP
exit /b 1`

1 Answer 1

1

The article you refer to does not contain the code that you are attempting.

The article shows that you need to create a Windows batch file (.bat) and use it first to run WinSCP to determine if a file exists. And then do something based on that finding. In your case, you will need to run WinSCP again to do the actual download. Something like this (not tested):

@echo off

set SESSION=mysession 
set REMOTE_DIR=/home/user
set REMOTE_FILE=test.txt
winscp.com /command ^
    "open %SESSION%" ^
    "stat %REMOTE_DIR%/%REMOTE_FILE%" ^
    "exit"
 
if %ERRORLEVEL% equ 0 (
    echo File %REMOTE_FILE% exists, will download it
) else (
    set REMOTE_FILE=other.txt
    echo Error or file %REMOTE_FILE% not exists, will download %REMOTE_FILE%
)

winscp.com /command ^
    "open %SESSION%" ^
    "get %REMOTE_DIR%/%REMOTE_FILE%" ^
    "exit"

Though from SSIS, I'd recommend you to instead use Script Task with C# (or other) code and WinSCP .NET assembly. See Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS).

In the code, you can use Session.FileExists method to determine if a file exists. This is also covered in the article that you refer to (although for PowerShell).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.