Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Using SSIS script task to create a text file

Posted on 2011-03-21
Medium Priority
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"
put "D:\SSIS\DATA\ARCHIVE\MyReport_20110321.zip"

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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 16

Accepted Solution

carsRST earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

636 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