Go Premium for a chance to win a PS4. Enter to Win


SSIS Script Task Error

Posted on 2012-03-28
Medium Priority
Last Modified: 2012-03-31
SSIS Script Task.

I am using a ForEach Container to get the file as a variable (User::foundFiles).   I am using it in a previously built DataFlow Task so I know it works just fine for that.

Within this Script it's throwing an error saying "Empty path name is not legal"

What am I missing?  Can't figure out why it's not working.


Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Empty path name is not legal.
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
   at ST_c71263698669469292bfc5b447ef5fa6.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()


using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_c71263698669469292bfc5b447ef5fa6.csproj
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        #region VSTA generated code
        enum ScriptResults
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        public void Main()
            string fileContent = System.IO.File.ReadAllText(Dts.Variables["foundFiles"].Value.ToString());
            string delimiterInfile = fileContent.Substring(103, 1); //103 because it's zero-based
            fileContent = fileContent.Replace(delimiterInfile, "*");
            string delimiterInfile2 = fileContent.Substring(104, 1); //104 because it's zero-based
            fileContent = fileContent.Replace(delimiterInfile2, ":");
            System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);
            Dts.TaskResult = (int)ScriptResults.Success;
Question by:simplyfemales
  • 6
  • 4
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37782029
Are you sure the 'OutputFile' variable's value is not empty and has correct path with file name?

Author Comment

ID: 37782320
At this point I'm not sure of anything and am a relative newb in Scripting.  The Script was supplied by someone else.  It builds just fine, but the error comes from the package execution.

What would you suggest the value be and how do I assign the path name to it?

Within the ForEach Container I have the Collection set to the proper folder path and it's instructed to collect *.txt and also include the fully qualified filename.

This is then mapped to the variable User::foundFiles (Package, String, no value)

OutputFile has been set up as User::OutputFile (Package, String, no value)

As indicated before, the foundFiles variable was previously used for a DataFlow task and worked just fine with multiple files in the directory (it goes through the directory and addresses each file independently without problem).  The intention of this script is to read each file in the directory (which should be identified as the User::foundFiles variable and looped through via the ForEach Container), replace the characters located at position 103 and 104 and then output the file so that they are then available to the DataFlow Task which previously used the User::foundFiles variable but has now been reassigned to use the User:OutputFile variable.

The package is failing at the Script so it's not even getting to the output.... or at least it does not appear to be making it that far.

FYI.  When looking at the Variable Mappings within the ForEach Loop Container, the Variable indicates User::foundFiles and the Index shows the number 0 (zero).

Author Comment

ID: 37782577
New info.  When I comment out the line

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

the script runs successfully.

What is occurring with the OutputFile line and how to fix it?
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37782611
You need to add a value to that variable, so the script can write the files to.

try setting a local path and see what would happen.

Author Comment

ID: 37782659
Huslayer.  I set a local path on the User::OutputFile variable c:\localpath\1.txt and it works just fine.

Now, this is supposed to write to a variable, not to a fixed location.  The variable is then supposed to be available to a DataFlow task.  How do I make that work?  I obviously don't want it to write to a new file, rather I want it available as the variable inside the Loop.
LVL 21

Accepted Solution

Jason Yousef, MS earned 1500 total points
ID: 37782716
2 ways, y u can write to a clean txt file like what you did, then use that as a clean source.

or you can use the script task as source...

there are many examples if you google it


Author Comment

ID: 37782994
huslayer.  A little bit confused.  If I commit the results of the script to a variable, then that variable should be available without it being defined as a "source" by a script, shouldn't it?

I'm using this in an EDI workflow, and more specifically, using the CozyRoc EDI Source within the DataFlow task.  It has the ability to utilize a variable source and so I have set it to use the User:OutputFile variable as it's source.

Obviously there's a disconnect occurring where the ControlFlow Script is not placing the information anywhere, even though it seems that it's being told to place it in the OutputFile Variable?  I'm not sure why it's not committing this value anywhere?  My assumption is that this Variable is supposed to just be random memory that gets committed and is available for use as a global variable but it isn't being populated by the script for some reason.

Anyways.  Excellent link and I have bookmarked it for future use, but as the EDI Source component can't receive any inputs in the normal fashion, I can't connect it to a Script Source.  I strictly have to use the variable.
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37783022
how big is the contents of the file?
I'm not familiar with the cozyroc but you could ask for their support or read on what type of variable is supported: string, ojbect or even text

Author Comment

ID: 37783105
file contents are variable.  1K up to 300K or so.

Sent an email to cozyRoc, but not sure anything gets past the point that I assume the code of this line -----

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

is telling the system to write the values within fileContent to the Variable OutputFile, correct?

If it's a variable, it should not need a fixed location, right?

If it's a variable, it's committed to memory rather than a physical location?

I'm missing something here, but I just don't know enough to know what it is.  I am stuck on the thought though that the variable is not a fixed value and should not need to be.
LVL 37

Expert Comment

ID: 37786905
Some explanation:

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

That line is creating a new file on the file system.  The content of that file is the content of the fileContent variable.  The path and location of the file is provided through the OutputFile package variable.

To be able to make the output file name dynamic, you should put only the path as value for the package variable, and pass another value into the script that can help you create a filename.  The User::foundFiles which you use in your loop would make a good candidate.

Possibly you'll need to do some further string manipulation, depending on how you set up the loop: "name only", "fully qualified", "name and extension"?

Assuming "name only", you could do this:

string outFile = Dts.Variables["User::OutputFile"].Value.ToString() + "\\" + Dts.Variables["User::foundFiles"].Value.ToString() + ".txt";
System.IO.File.WriteAllText(outFile, fileContent);

The above assumes your OutputFile package var does not end in a backslash and that foundFiles contains the name of the file without extension.

Author Closing Comment

ID: 37792131
turns out the source needs to reference a fixed file and not a variable.  I dumped the variable to a fixed temp file and it overwrites each time as the loop works its way through.

Thanks for the help

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

879 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