Monday, January 11, 2016

Find empty file using C# and script task

While processing different types usually the issues occurs file empty!! It causes failure of package in production. to get rid of this issue one way is to find file with empty content and delete them.

As an input we do require file name and flag that states file is empty or not.

Best way to deal with multiple file is to run through for each loop container and check the file content. If file is empty than based on expressions in control flow using Emptyfileflag value we can delete that file using File system task.

            string path = Dts.Variables["User::FileName"].Value.ToString();    

            StreamReader sr = File.OpenText(path);
            string scontent = sr.ReadToEnd();
            sr.Close();          

            if (scontent.Length == 0)
                {

                    Dts.Variables["User::Emptyfileflag"].Value = true;
                }
            else
                {
                    Dts.Variables["User::Emptyfileflag"].Value = false;
                }
           
Dts.TaskResult = (int)ScriptResults.Success;
}

No comments:

Post a Comment