Using SSIS script task to create a text file

Posted on 2011-03-21
Last Modified: 2013-11-10

I need to my SSIS package to create a dynamic text file on the local file system that will be referenced by a subsequent Execute Process task. Specifically I want my Execute Process task to run the WinSCP process to use a script file (stored as a plain text file) to upload a zip file to an sftp server. The problem is that the name of the zip file changes every day according to the current date so I need the WinSCP script file to change its contents every day to make sure the correct zip file is uploaded to the sftp server.

My WinSCP script file will contain something like this:
option batch on
option confirm off
open sftp://Me:Password@ -hostkey="ssh-rsa 1024 68:87:fa:79:2f:70:2c:28:f0:3f:a7:3b:9a:16:b9:00"

It is the put statement that needs to be dynamic.

If we say that the WinSCP script file is "D:\SSIS\WinSCP\Scripts\MyReportScript.txt" can someone please give me detailed instructions on how to write a script task that will delete and recreate "D:\SSIS\WinSCP\Scripts\MyReportScript.txt" every day, using a pre-set package level variable (lets call it MyReportFileName) to construct the put statement? I think the return characters to terminate each line are important too.

I know how to delete the file if it exists using the following (VB) code:
If File.Exists("D:\SSIS\WinSCP\Scripts\MyReportScript.txt") Then
End If

But when it comes to writing lines to a new text file, I'm not sure what to do. Your help will be much appreciated!

Thanks in advance.
Question by:irb56
  • 2
LVL 16

Accepted Solution

carsRST earned 500 total points
ID: 35183699
You'll probably have to use scripting.  Few options.

One option is to create the text file and just replace the necessary data.
Add a script task and use function below (code window) - translate to VB.NET if needed.  You can use it to open your file, change the contents, save and close the file.  Want I would do is set up a dummy line in your text file that the function can easily find and just replace.

If you're passing in a variable, you'll need to set the property on your script task ReadOnlyVariables and select the one or ones you plan on using.  Below is the line you can use to read that variable in your script.
Dts.Variables["User::VariableName"].Value (where "VariableName" is the name of your variable)

After your process you can change your text file back to your dummy statement, so that the next time it runs it can look for that line to replace.  Really it's just a matter of copying out your script file and reversing the process in the code.

Other alternative is to create the file every time.  More work, in my opinion.  But you can use the "File System" task to delete a file.  Add that task and change the operation to "Delete File."

Then just write  the code, in a script task, to create the text file each time.  Below is basic .NET code to write out to a text file.

//replace function
        static public void ReplaceInFile(string filePath, string searchText, string replaceText)


            StreamReader reader = new StreamReader(filePath);

            string content = reader.ReadToEnd();



            content = Regex.Replace(content, searchText, replaceText);


            StreamWriter writer = new StreamWriter(filePath);




Open in new window

LVL 16

Expert Comment

ID: 35184711
If you are able to use Robo-FTP instead of WinSCP as the SFTP client you could have the script logic calculate the filename based on the current date like this:
;; build expected filename
SETEXTRACT day = %date "-" 1
SETEXTRACT month = %date "-" 2
SETEXTRACT year = %date "-" 3
SET ReportName = "MyReport_20" + year + month + date + ".zip"

GETFILE ReportName  ;; Is file actually here?
FTPLOGON "" /servertype=SFTP /user="Me" /pw="Password" /trust=all


Open in new window

... or if you are certain you want to figure the filename to be sent in an external process you could use a tiny script that reads the name of the file to send from a command line argument like this:
TPLOGON "" /servertype=SFTP /user="Me" /pw="Password"
SENDFILE %1 ;; note: first command line arg is autosaved as %1

Open in new window

LVL 16

Expert Comment

ID: 35184828
first command in that second script should have been FTPLOGON instead of TPLOGON

Author Closing Comment

ID: 35188518
Thanks for the responses. I'm quite happy with WinSCP as the SFTP client in use in my SSIS packages so I went with the script task to recreate the WinSCP script file. For reference, the code (VB) was as below. The package variable "FinalReportName" was preset outside of the script task. It all works just fine. Many thanks for your assistance!  :-)

If File.Exists("C:\SSIS\MyReport_SFTP_Upload_Script.txt") Then
        End If

        Dim putLine As New String("put " & Dts.Variables("FinalReportName").Value)
        Dim writer As New StreamWriter("C:\SSIS\MyReport_SFTP_Upload_Script.txt")
        writer.WriteLine("option batch on")
        writer.WriteLine("option confirm off")
        writer.WriteLine("open sftp://Me:Password!@ -hostkey=""ssh-rsa 1024 68:87:fa:79:2f:70:2c:28:f0:3f:a7:3b:9a:16:b9:00""")

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query Task 11 44
Building JSON Results Table FROM DB 9 37
Need help with another query 10 39
Data encryption options between SQL DBs 3 32
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question