Lee posted on August 12, 2011 12:57

I had a post earlier for counting files in a folder, but it was DOS and could be called from a process task...that's really not the way that I'd do it I guess, so don't use that example.  I see plenty of hits for folks searching and looking at that page, so I offer up this as an alternate. If I were tasked to do this in SSIS I'd probably use something like the following.

Crack open SSIS, add a Script Task to the Control Flow, create two SSIS variables, one called Folder of type string, and another named Count of type integer, and then add this code to the script task:

 

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
 

namespace CountFilesInFolder
{
    [System.AddIn.AddIn("ScriptMain", Version="1.0",Publisher="",Description="")]
    public partial class ScriptMain
           : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
 
        public void Main()
        {
            string foldername;
            int count;
 
            foldername = Dts.Variables["Folder"].Value.ToString();
            count = Directory.GetFiles(foldername).Length;
           
            MessageBox.Show(count.ToString());
            Dts.Variables["Count"].Value = count;
 
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}


Directory.GetFiles(foldername).Length will count files for you in .Net/c# probably as good as any method, so use this and not the garbage that I posted in the other blog.  Remember, I geek here, so take this stuff (including this post) at face-value.

 

 

Thanks,
Lee

 

----------------------------

 

MELINDA - Well, I just thought I'd give you them. I liked walkin' with you. I got a blister the size of a quarter on one heel. Well, I'll see you sometime, I guess. (She walks to the door and stops as if she expects Karl to say something.)

KARL - A blister shore can hurt.

 

 


Posted in: .Net , SSIS  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Info

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog