Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

SSIS scenarios...
Browse by Tags · View All
MSBI 30
SSIS 27
BRH 15
SQL Server 15
#BI 14
Script Component 9
#SQLSERVER 8
#SQL Server 7
Flat File Source 5
Script Component Source 5

Archive · View All
August 2010 4
March 2011 3
September 2010 3
February 2013 2
June 2011 2
November 2010 2
May 2010 2
November 2009 2
March 2010 2
May 2012 1

SSIS - Expression to get File Name from File Path

Nov 22 2010 6:15PM by Sudeep Raj   

Scenario: How to extract the file name from a file path where the number of sub folders or name of folder is not fixed. In simple terms you do not know the position where the file name starts.

Typical example: You have a foreach loop set to traverse sub folders and you set the retrieve file name to "fully qualified" and in parameter mapping you map this to a string variable strFilePath. You use this variable to set up the connection string for may be a flat file source(will not be discussing that). Now lets say you need to retrieve the file path for logging purpose or sending a mail. How do we do this??

Solution: Have another variable and name it as strFileName. Goto the properties of this variable by selecting the variable and clicking F4. Set the property EvaluateAsExpression to true. The Expression property becomes enabled. Put the below expression there:

REVERSE(SUBSTRING(REVERSE( @[User::strFilePath] ), 1, FINDSTRING(REVERSE( @[User::strFilePath] ),"\\", 1)-1))

Explanation:

Lets say our file path is: C\Folder1\folder2\file.txt

Lets break this expression into parts -

REVERSE
(
	SUBSTRING
	(
		REVERSE( @[User::strFilePath] ),
        1,
        FINDSTRING
        (
			REVERSE(@[User::strFilePath] ),
            "\\",
            1
        ) -1
    )
)

If we use the expression REVERSE(@[User::strFilePath]) it will reverse the string within the parenthesis in our case the file path. So the output of this segment would be:

" txt.elif\2redlof\1redloF\C" Let us assume the output of this expression REVERSE(@[User::strFilePath]) = revPath for simplicity sake.

Now our work becomes simple just find the first instance of back slash and read the string from the first position to the first instance of a back slash.

Now what would be the expression to do the above: SUBSTRING(revPath, 1, FINDSTRING(revpath, "\\",1)-1 )

If you notice after the Findstring I do a minus one that is done to remove the back slash else even that would be there in the output which is not required.

The output we get from the above expression is: " txt.elif"

I hope now things look simpler all we need to do now is do another REVERSE function call to reverse the above string to file.txt

UPDATE

Thanks to Mario Puskaric, a simpler expression for this is: 

RIGHT(@[User::strFilePath],FINDSTRING(REVERSE(@[User::strFilePath]),"\\",1)-1)

Tags: SSIS, SQL Server, Expressions, For EAch loop, MSBI, #BI, #SQLSERVER, BRH, File Name, File Path,


Sudeep Raj
12 · 13% · 4287
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Why do You complicate (spend processor time), when RIGHT answer is:

    RIGHT(@[User::strFilePath],FINDSTRING(REVERSE(@[User::strFilePath]),"\",1)-1)

    commented on Dec 29 2010 2:19AM
    Mario Puskaric
    150 · 1% · 321
  • Just one doubt: Now that I have the variable with file name inside, how can I show it? I tried a .NET script task and BIDS returned an error saying that the entry point was wrong.

    Here is the code I used now, using C#:

    I'm brazilian, so "Arquivo lido" means "read file".

    //-------------------------------------------------------

    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_0bcf49003cfc417397b247059983adf9.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
            };
            #endregion
    
            public void Main()
            {
                MessageBox.Show("Arquivo lido: " + Dts.Variables.ToString(), "B2W - SSIS");
                Dts.TaskResult = (int)ScriptResults.Success;
            }
        }
    }
    

    //-----------------------------------------------------------------------------

    this code is working, but the message box returns the follow string: "Arquivo lido: Microsoft.SqlServer.Dts.Runtime.Variables" It's the name of the class? What should I update to return the file name?

    Tks.

    Kdu Bonalume - Brasil

    commented on Dec 13 2011 11:48AM
    Kdu Bonalume
    2895 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

"SSIS - Expression to get File Name from File Path" rated 5 out of 5 by 1 readers
SSIS - Expression to get File Name from File Path , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]