CSV export: dynamic generation of file names
- FlowHeater-Team
- Topic Author
- Offline
- Admin
Less
More
13 years 11 months ago #2073
by FlowHeater-Team
Best wishes
Robert Stark
Did this answer your question? We would be grateful if you provide a brief comment as feedback. It may also help others who may have encountered a similar problem.
CSV export: dynamic generation of file names was created by FlowHeater-Team
Translated submission in the German forum. You can read the original entry
here
.
Hello,
We are exporting CSV data from an SQL Server and would like to dynamically generate the exported CSV file names.
In our case the exported data contains a date field, which we could use to compose the file name using the month and year.
Is there a straightforward way to address this requirement?
Many thanks in advance,
J.-U. Lein
Hello,
We are exporting CSV data from an SQL Server and would like to dynamically generate the exported CSV file names.
In our case the exported data contains a date field, which we could use to compose the file name using the month and year.
Is there a straightforward way to address this requirement?
Many thanks in advance,
J.-U. Lein
Best wishes
Robert Stark
Did this answer your question? We would be grateful if you provide a brief comment as feedback. It may also help others who may have encountered a similar problem.
Please Log in or Create an account to join the conversation.
- FlowHeater-Team
- Topic Author
- Offline
- Admin
13 years 11 months ago #2074
by FlowHeater-Team
Best wishes
Robert Stark
Did this answer your question? We would be grateful if you provide a brief comment as feedback. It may also help others who may have encountered a similar problem.
Replied by FlowHeater-Team on topic Re:CSV export: dynamic generation of file names
Hello Mr Lein,
You can do this with the .NET Script Heater and the following short C# .NET Script.
Drag and drop a .NET Script Heater onto one of the existing connections.
Please note: This step is necessary, because without a connection to the WRITE side the script code it contains is never executed!
Now format the CSV file name as you require from your date field using Heaters/Functions and connect the dynamically generated string to the second input of the .NET Script Heater. The script will now set the CSV file name in the WRITE TextFile Adapter to this new name during the transform. The first input to the .NET Script Heater is passed through unaltered.
WARNING: This method only functions when using the Mass Data mode.
I have prepared a brief example for your requirement, see the enclosed "dynamic_csv_export_file_names.zip" file. The example uses the TestData Adapter on the READ side, just creating a handful of test rows for the purposes of illustration. The CSV file names are dynamically generated from a date field and formatted using the Format Heater. The String Append Heater concatenates all elements of the CSV file name together and passes the complete CSV file name to the second input of the .NET Script Heater.
Note: This example also demonstrates how several CSV text files can be written during a single export run. If the incoming dates differ and are not sorted in sequence (as in the example), you also need to check the option "Append to file" in the TextFile Adapter on the WRITE side.
You can do this with the .NET Script Heater and the following short C# .NET Script.
Code:
public object DoWork()
{
TextFileAdapter adapter = (TextFileAdapter)AdapterWrite;
if (InValues.Length != 2)
throw new Exception("2 input parameter required");
// do not execute in test mode
if (adapter.OnlyTest == false)
{
// Set the new file name in the TextFile adapter = second input parameter
// WARNING: This method only functions when using the Mass Data mode.
adapter.Filename = (string)InValues[1].GetString();
}
// just returning the first input parameter
return InValues[0].GetValue();
}
Drag and drop a .NET Script Heater onto one of the existing connections.
Please note: This step is necessary, because without a connection to the WRITE side the script code it contains is never executed!
Now format the CSV file name as you require from your date field using Heaters/Functions and connect the dynamically generated string to the second input of the .NET Script Heater. The script will now set the CSV file name in the WRITE TextFile Adapter to this new name during the transform. The first input to the .NET Script Heater is passed through unaltered.
WARNING: This method only functions when using the Mass Data mode.
I have prepared a brief example for your requirement, see the enclosed "dynamic_csv_export_file_names.zip" file. The example uses the TestData Adapter on the READ side, just creating a handful of test rows for the purposes of illustration. The CSV file names are dynamically generated from a date field and formatted using the Format Heater. The String Append Heater concatenates all elements of the CSV file name together and passes the complete CSV file name to the second input of the .NET Script Heater.
Note: This example also demonstrates how several CSV text files can be written during a single export run. If the incoming dates differ and are not sorted in sequence (as in the example), you also need to check the option "Append to file" in the TextFile Adapter on the WRITE side.
Attachment dynamic_csv_export_file_names.zip not found
Best wishes
Robert Stark
Did this answer your question? We would be grateful if you provide a brief comment as feedback. It may also help others who may have encountered a similar problem.
Attachments:
Please Log in or Create an account to join the conversation.
Time to create page: 0.276 seconds