Blog Post

Automate Back Slashes in Your File Path Expressions in SSIS

,

One of the most aggravating things when work with the SSIS expression language is when you have to combine file names and losing consistency because you forgot to put backslashes in and the filename is wrong. :)

I have hit this wall a bunch of times and I use a trick I discovered awhile ago to make sure this doesn’t happen. It’s not earth shattering, but it will help make sure you avoid this.

Normally the expression to combine filename and filepath looks like this:

   @[User::StrFileLocation] + @[User::StrFileName] Or  @[User::StrFileLocation] + "\\" + @[User::StrFileName]

Instead we can add a snippet to the middle to check for the existence of the slashes and either put them in or not.

+(RIGHT(@[User::StrFileLocation], 1) == "\\" ? "" : "\\") +

Making the whole expression look like this:

  @[User::StrFileLocation] +  (RIGHT(@[User::StrFileLocation], 1) == "\\" ? "" : "\\") + @[User::StrFileName]

There are two slashes because the first one is an escape character to allow the \ to be read as a slash. The right (1) only checks for the single slash since this is included. :)

This should help you avoid at least one more pesky debugging task :) . Happy coding.

As always, please post questions to the BIDN forums.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating