SSIS Expression to append file extension - sql-server

I'll get a Full filepath(FilePath and FileName) from a variable (#[User::V_FullPath]) as C:/Users/ABCD/Documents/Development/SampleFile.txt
I have a file with the same name but with .xlsx (SampleFile.xlsx) in another folder(A) that I want to copy to another folder(B)
To get just the filename I'm using the expression:
SUBSTRING(#[User::V_FullPath],37,47)
How can I append .xlsx to the above expression
My goal is to get SampleFile.xlsx

Why not just replace .txt with .xlsx?
REPLACE( #[User::V_FullPath]),".txt",".xlsx")
This will result in the following value:
C:/Users/ABCD/Documents/Development/SampleFile.xlsx
If you need only the filename Sample.xlsx, you can use TOKEN and TOKENCOUNT functions as follows:
TOKEN(TOKEN(#[User::V_FullPath],"/", TOKENCOUNT(#[User::V_FullPath],"/")), ".", 1) +".xlsx"
Expression result:
Sample.xlsx
Similar questions:
How do I extract file name from a path within an SSIS package?

Related

SSIS package how to add date/timestamp to file name each time package run

i have a SSIS package that outputs a csv file to a location this package will be run on a daily basis.at the moment I look if a file exists and if it does I delete it before creating a new one so that I don't get any errors with creating a file that already exists - what I want to do is create a new csv and add a date/timestamp to the end of file name. I'm not sure how I go about achieving this.
You can achieve this using the following steps:
Add a variable i.e. User::Filename
Click on the variable, and press F4 to show the properties Tab
In the Expression use an expression similar to the following:
"C:\\Filename_" + (DT_WSTR,4)YEAR(GETDATE()) + "_" + (DT_WSTR,4)MONTH(GETDATE()) + "_" + (DT_WSTR,4)DAY(GETDATE()) + ".csv"
In the Destination Flat File Connection manager, in the expressions (found in the property tab) use this variable as a Connection String expression
#[User::Filename]

How to Import Just The FileName into SQL via SSIS

I am trying to write a file name to a table in my database - at the moment all I am achieving is importing the whole file path.
I have a foreach loop, which on the Collection looks in a specific folder and for a specific file type (the retrieve file name is fully qualified)
This has a variable mapping to "ImportInvoiceFilePath"
Then within that is a Data Flow Task which includes the flat file source, a derived column which creates the file path in the database.
This works fine - but what I am trying really hard to do but can't work out is how do I get just the file name (no extension) to write to the database as well?
Literally worked it out. Set my forloop to nameonly then in my connection to my Source file under expressions put:
#[User::ProcessingInvoiceFilePath] + "\\"+#[User::ImportInvoiceFileName]+".saf"
Where saf is the file type

SSIS :- variable fof excel filename

i have a folder containing multiple excel file. Excel files name are almost same except every file name contain month and year number in last.
Example
Emp_04_2017.xlsx
Emp_05_2017.xlsx
...
I want to create a SSIS package that pick the current month file and insert it into the destination table.
One way would be to create SSIS variables to store the current month and year, and then use those to construct the name of the file in a third variable.
Use a ForEachLoopContainer.
ForEach Loop Container will pick files from the FolderPath variable and return complete CompletePath (Path+fileName). Loop will iterate through all the files in the FolderPath location.
Foreach Loop Container: Double click -> In Collection set Expression Directory = FolderPath, Enumerator Configuration -> Files : (.xlsx).
Vaiable Mappings -> Variable (CompletePath) Index 0.
EXPR_GetFirstOcrDash: Expression used to get first occurrence of dash in filename, #[User::FirstOcr] = FINDSTRING(REVERSE(#[User::CompletePath]), "_", 1).
EXPR_ExtractFileName: The expression is used to get month from the file name, #[User::FileMonth] = (REVERSE(SUBSTRING(REVERSE(#[User::CompletePath]), #[User::FirstOcr]+2, 1)) == "0" ?
REVERSE(SUBSTRING(REVERSE(#[User::CompletePath]), #[User::FirstOcr]+1, 1))
:REVERSE(SUBSTRING(REVERSE(#[User::CompletePath]), #[User::FirstOcr]+1, 2)))
EXPR_SetFileToProcess: Used to set the file which we found for processing, #[User::FileToProcess] = #[User::CompletePath]
EXPR_StopProcessing: The loop will continously check all files in the folder, when we found first file with month of current date, we will not further look for file.
A better practice could be use two directory Source and Archive, once file is processed move the processed file to Archive directory using FileSystemTask.
Precedence Constraints are added on the green arrows.
After the Foreach Loop Container gets processed, you can use FileToProcess variable and use the file in the DataFlowTask.

Logparser - remove file extension

The below logparser query is returning the file names accessed from a website.
LogParser.exe -i:W3C "SELECT EXTRACT_FILENAME (cs-uri-stem) As FileName FROM c:\weblog\file1.log" -o:CSV
Example returned:
forum.aspx
example_image.jpg
search.aspx
Can someone provide insight on how to remove the extension of the file names from within logparser?
You can use the EXTRACT_PREFIX function to extract everything up to the last substring separated by '.':
SELECT EXTRACT_PREFIX( EXTRACT_FILENAME (cs-uri-stem), -1, '.')...

SSIS retrieving current folder within a foreach loop traversing subfolders

I use SSIS to read .txt files in input and execute my business logic over them saving the output results in a file whose name is the same as the current inpout file (file name dynamically stored in a variable).
When all the files are stored in the same folder, I have no problem accessing them since I use the following expression for the flat file connection string in the data flow: "path" + #[User::inputFileName] + ".txt"
Now I have to process a folder with subfolders (I set traverse subfolders in the foreach loop) and I have some issues with the flat file connection string since I cannot use a wildcard like: my path\\subfolder*" + #[User::inputFileName] + ".txt" where every subfolder has same name and changes only the last portion of the name.
How can I save the current subfolder name in a variable so that I can use it in the following way? "path\\"+ #[User::currentSubFolder] +"\\" + #[User::inputFileName] + ".txt"
I was able to solve my issue, therefore I write here my solution in the case someone else would be in the same situation.
I used a script transformation block before my foreach loop. From it I can retrieve the current full path (used afterwards in the Flat File connection string) and the input file name without extension to be used as output file name containing the results of the SSIS scripts.
In order to keep the values of interests I used 2 variables: one for the file name and one for the path.
Here the script code:
Public Sub Main()
'Variable Index 0 => FileName
'Variable Index 1 => filePath
Dim fullPath As String = Dts.Variables.Item(1).Value.ToString
Dim fileName As String = Path.GetFileName(fullPath)
fileName = fileName.Substring(0, fileName.Length - 4)
Dts.Variables.Item(0).Value = fileName
Dim x As String = Dts.Variables.Item(0).Value.ToString
Dts.TaskResult = Dts.Results.Success
End Sub

Resources