Recently, I had built an SSIS package that searches my TFS structure after getting the latest version. I had many files I needed to bring over to one location; so to do this I built out a simple SSIS package. I failed to realize that when I bring my files down from TFS to my local environment they are protected as read only. This causes me a problem as I am not able to clear out my repository directory ~ you will inevitably get a “permission denied” error.
To prohibit manual intervention every time I perform this action I tacked on a Script Task property to my package. The basic flow of the package is as follows:
- Check if my landing zone (repository) is available. If it is then use the existing directory if it is not than create it.
- If there are any files in the directory prior to bringing over my new files than obliterate them from the directory.
- Grab 16 various files from different locations and drop them into the landing zone.
- Turn off the read only attribute of the files brought over.
- Rinse and repeat as needed.
It is in step 5 where I turn off the read only attribute. As you can see the file system task in my package is below:
By double clicking the Script Task you will be taken to a Script Task Editor screen. Simply click on “Edit Script”:
After clicking on the “Edit Script” button you will be taken to an SSIS Script editor. The below code provides a simple way of removing the read only attribute; in my beta version I have left the value hard-coded but eventually I would like to pass this is as a parameter so that I won’t have to come inside the code to change it. Also note that you can swap out the false with true and turn the read only attribute back on.
public void Main()
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@”C:\INRepository”);
foreach (System.IO.FileInfo fi in directory.GetFiles())
fi.IsReadOnly = false; // or true