2

I'm using SSIS and a script task in C#.

I have a FOR EACH Loop that looks into a folder and assigns the full path and file name of each file found to a variable called FileNameFound which in turn is then assigned to a variable called filepath within the Script Task.

I also have a Project parameter that stores an output path, this is being assigned to 'newFilepath' within the Script Task.

Using some other code I then Encrypt the csv file.

Bottom line is, I need the output to go into a different folder then where it was found but retain the same filename.

e.g.

  • find a file: c:\folder\test.csv
  • encrypt and output to c:\folder\new folder\test.csv

I need newFilepath to be the variable $Project::CSV_OutputFolder + test.csv

I am trying to incorporate GetFileNameWithoutExtension(String) but keep receiving the error:

The name 'GetFileNameWithoutExtension' does not exist in the current context

This is my code:

public void Main()
{
    // Get the filepath of the file that needs to be encrypted.  
    string filepath = Dts.Variables["FileNameFound"].Value.ToString();

    // build output path
    string newFilepath = Dts.Variables["$Package::$Project::CSV_OutputFolder"].Value.ToString() + GetFileNameWithoutExtension(Dts.Variables["FileNameFound"].Value.ToString())+ ".csv";

    // Get password from SSIS variable
    string encryptionKey = Dts.Variables["EncryptionKey"].ToString();

    // Create an encrypted copy of the file
    Encrypt(filepath, newFilepath, encryptionKey);

    // Close Script Task
    Dts.TaskResult = (int)ScriptResults.Success;
}

What am I missing?

Hadi
  • 36,233
  • 13
  • 65
  • 124
Michael
  • 2,507
  • 8
  • 35
  • 71

2 Answers2

2

I got it working. I change the For Each to just retrieve the filename only then amended my code:

public void Main()
{
    // Get the filepath of the file that needs to be encrypted. 
    string filepath = Dts.Variables["Unencrypted_Folder"].Value.ToString() + Dts.Variables["FileNameFound"].Value.ToString();

    // build output path
    string newFilepath = Dts.Variables["$Project::CSV_OutputFolder"].Value.ToString() + Dts.Variables["FileNameFound"].Value.ToString();

    // Get password from SSIS variable
    string encryptionKey = Dts.Variables["EncryptionKey"].ToString();

    // Create an encrypted copy of the file
    Encrypt(filepath, newFilepath, encryptionKey);

    // Close Script Task
    Dts.TaskResult = (int)ScriptResults.Success;
}
Michael
  • 2,507
  • 8
  • 35
  • 71
0
Path.GetFileNameWithoutExtension(Dts.Variables["FileNameFound"].Value.ToString()) 

as it is static class in System.IO namespace.

JMP
  • 1,864
  • 2
  • 8
  • 16