Reputation: 13
I am new To SSIS packages and I am trying to create a SSIS script task which creates an empty file and file name should be like "FileName__yyyymmdd__.csv". I am trying to compile this code but it is taking ages to compile and isn't even showing any error but when I execute the package as whole, the package gets failed at script task.
The code below is written in Main function:
Dts.TaskResult = (int)ScriptResults.Success;
String FileLoc="D:\\Folder\\" ;
String BaseName="FileName__";
DateTime dt= DateTime.Now;
String yr= dt.Year.ToString();
String mm= dt.Month.ToString();
String dd= dt.Day.ToString();
String FileName = BaseName + yr + mm + dd + "__"+".csv";
File.Create(FileLoc+FileName).Dispose();
Could anybody please highlight the error? Thank you.
Upvotes: 1
Views: 91
Reputation: 20975
The filename can be generated in 1 line.
Also, you can set a breakpoint in your Script task and then execute the package in Debug mode, after selecting the package as the Startup package.
You are doing a couple of things incorrectly (or inefficiently). I've corrected your code. Try the following code. It works.
You'll need the following using
statements on top of your script .cs
file.
using System;
using System.IO;
using System.Globalization;
And inside your main function, the following code as appropriate.
string fileLocation = @"D:\Folder\" ;
string fileNamePrefix = "FileName__";
string filename = Path.Combine(fileLocation, fileNamePrefix
+ DateTime.Today.ToString("yyyyMMdd", CultureInfo.InvariantCulture)
+ "__.csv");
// This will print "D:\Folder\FileName__20160920__.csv".
Console.WriteLine(filename);
// if file does not exist, create it.
if (! File.Exists(filename))
{
using (StreamWriter sw = File.CreateText(filename))
{
// write data into file using sw.WriteLine(...) or something similar
}
}
Dts.TaskResult = (int)ScriptResults.Success;
Upvotes: 1