How do I extract file name from a path within an SSIS package?

拜拜、爱过 提交于 2020-01-17 08:43:10

问题


I would like to know how to extract the file name using SSIS from the below given paths:

Paths:

O:\SourceFiles\Primary\care\ALC121.csv
O:\SourceFiles\Primary\care\COPL119.csv

I would like to insert the names and the full path into a table as shown below:

Table data:

QueryName  FileName 
---------  ---------------------------------------
ALC121     O:\SourceFiles\Primary\care\ALC121.csv
COPL119    O:\SourceFiles\Primary\care\COPL119.csv

回答1:


I'm assuming that the full file names are coming in off the ForEach Enumerator for files. It seems the request then is to reduce that string down to just the base file name. Some folks will use expressions to get you there but I find that an ugly approach. Instead, I'd use a script task and take advantage of the functionality in the .NET library.

System.IO.Path.GetFileNameWithoutExtension

Returns the file name of the specified path string without the extension.

Create a second variable, call it QueryName with a type of String and set it as a read/write variable in the script. Assign the value of it based on the results of that metchod call with the FileName value.

Then use an Execute SQL Task to insert them into your table. Use a ? as a parameter.




回答2:


File name without extension :

select substring(?,len(?)+1,(charindex('.',?)-1)-LEN(?))

for 4 parameter mapping
0 - FullPath
1 - FilePath
2 - FullPath
3 - FilePath




回答3:


Within a derived column, you can use TOKEN() and TOKENCOUNT() functions to do that:

TOKEN(
     TOKEN(@[User::Filepath],"\\", TOKENCOUNT(@[User::Filepath],"\\"))
, ".", 1)


来源:https://stackoverflow.com/questions/14880370/how-do-i-extract-file-name-from-a-path-within-an-ssis-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!