Setting the folder path for text files at run time
To access a text file, one of the properties that you must specify is the path to the folder that contains the text file. There are cases when the path to the folder can be determined only at run time. Consider the following scenario: A log file named log.csv is generated daily. Each daily log is stored in an auto-generated folder whose name is the current date. Examples of the full path to these folders are as follows:
C:\Logs\2011-07-01
C:\Logs\2011-07-02
C:\Logs\2011-07-03
You design a report that displays data from log.csv. When the report is run, the report uses the log.csv data for the current day. For example, if the report runs on July 1, 2011, the flat file data source uses the log.csv file in the 2011‑07‑01 folder. If the report runs on July 2, 2011, the data source uses the log.csv file in the 2011‑07‑02 folder. The following procedure shows how to write a JavaScript expression that returns the full folder path value based on the current date.
How to set the folder path for text files at run time
This procedure assumes that you have already created a flat file data source.
1 In Data Explorer, right-click the flat file data source, then choose Edit.
2 In Edit Data Source, choose Property Binding. The Property Binding page displays the flat file connection properties that you can set at run time.
3 Choose the expression builder button to the right of Home Folder.
4 In the expression builder, type the following expression:
function DF(n) {
return (n > 9 ? n : '0' + n);
}
 
var d = new Date();
shortDate = (d.getFullYear() + '-' + DF(d.getMonth() + 1) + '-' + DF(d.getDate()));
 
HomeFolder = "C:\Logs\"+shortDate;
The JavaScript functions, getFullYear( ), getMonth( ), and getDate( ), get the parts of the full date and time returned by new Date( ). The user-defined DF function formats the month and day parts of the date. The shortDate variable contains the date in 2009-01-01 format. The HomeFolder variable contains the full folder path, which is constructed by concatenating the static path with the shortDate variable.
5 Choose OK to save the expression.
Edit Data Source shows the JavaScript expression bound to the Home Folder property, as shown in Figure 1‑9.
Figure 1‑9 JavaScript expression bound to the Home Folder property
6 Choose OK to save your changes to the data source.