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.

Software ==> SSIS Integration Services  (46 exercises)
Version ==> SSIS 2012 and later
Topic ==> Accessing file attributes  (2 exercises)
Level ==> Harder than average
Subject ==> SSIS training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

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:

Four files

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:

Total file size

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:

Variables to add

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:

Foreach loop container

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:

Variables in 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

using System.IO;

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:

Final script task

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:

The final message

Yeah - the right result!


Close your package down, reflecting on how you might use this in "real" life. 

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post