Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script Component


Script Component

Author
Message
lzlzgh
lzlzgh
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 145
I am trying to get the file name with a for each loop container and a script component but I receive an error on the script component: Below is the script:

Public Sub Main()
Dim FileName As String
Dts.Variables("User::FileName").Value = System.IO.Path.GetFileName(Dts.Variables("User::FileName").Value.ToString())
Dts.TaskResult = Dts.Results.Success

End Sub
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16518 Visits: 13210
What is the error that you get?

And why do you create a variable Filename that you don't use?

The following solution should work:

map the SSIS variable FileName in the For Each Loop to index 0 (see variable mappings).
Then, in your script component, use this code (make sure you add the variable FileName is a read-only variable in your script component/task):

Public Sub Main()
Dim FileName As String
FileName = Dts.Variables("User::FileName").Value.ToString()
Msgbox(Filename) 'To test it
Dts.TaskResult = Dts.Results.Success

End Sub




Note: if you use a script component, you should use Me.Variables.FileName to access the variable.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5812 Visits: 7140
From what you have stated, I presume you are running through a folder with numerous (1 or more) files to be imported.
when you create a foreach loop to iterate through a folder, you will have created a variable to hold the file name (eg @User::FileName).

thus, for each iteration, the variable is populated with the relevant file name being retrieved.
this variable can then be used in any subsequent process (constraint in the process flow, in a conditional split, added to the list of columns using a derived column component and captured in a table, etc.

the question to be answered therefore: what is it you need to achieve with the file name?

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Attachments
ForEachLoopExample.zip (14 views, 59.00 KB)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search