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;
}

Issues with space in file and folder name while passing as arguments in Execute Process Task

After working on VB.NET or C#.net console applications or other shell script, you had created executable. But while working on that you find issues with space while passing file name or folder name or any other string.

To resolve this issue its requires to make certain changes in your passing parameter style. better to put " \" " on both side of string which resolve this issue.

Before: 
        @[User::path]

After:
      " \"" + @[User::path] + " \""

If you have three strings as parameters than it should be like this:

   " \"" + @[User::path] + " \"" + " \"" + @[User::path1] + " \"" + " \"" + @[User::path2] + " \""