WISE OWL EXERCISES
SSIS INTEGRATION SERVICES EXERCISES
- Data flow tasks (4)
- Basic data transforms (1)
- Data conversion transforms (2)
- Conditional split transforms (5)
- Lookup transforms (4)
- Looping over files (3)
- Looping over rows (2)
- Merge joins (1)
- Previous versions (18)
- Script tasks (1)
- Variables in script (1)
- Script components (2)
- Accessing file attributes (2)
SSIS Integration Services | Accessing file attributes exercise | Work out the total size of all files in a folder using a script task
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You can learn how to do this exercise on the relevant Wise Owl classroom training course (sadly for the moment only in the UK).
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
The above folder has 4 files in:
The four files in the above folder. Your task is to add the file sizes together, and report back!
If you right-click on the files to show their properties, you should see this:
The total size for all of the files is 210,304 bytes - something we intend to prove with an SSIS package!
To start, create a new package called File Sizes, and add the following variables to it:
We'll use the first variable to accumulate the total file size (set it to 0) and the second one to hold the file name/path for each file in turn.
Create a Foreach Loop container to loop over the files in the above folder, calling a script task for each:
Remember to assign your FilePath variable to the Foreach Loop container, to tell SSIS where each file name/path will be stored.
Assign your variables to the script task:
The script will need to read each variable's file path (so this is read-only), but will need to add to the cumulative size (so this is read-write).
In the script for the script task, remember to reference the files and folders namespace:
// reference namespace
Now write the script to get a pointer to the file whose name and path are stored in the FilePath variable, get the size of this file (using its Length property) and add this to the CumulativeSize read/write variable. Here are a couple of lines your code could include:
// get a reference to this file
var thisFile = new FileInfo(thisFilePath);
// get the size of this file
long fileSize = thisFile.Length;
When you've finished, add another script task to report what you've found:
The final script task should display the total file size accumulated in the variable.
Here's what your package should show when you run it:
Yeah - the right result!
Close your package down, reflecting on how you might use this in "real" life.