get the file size and zip it RRS feed

  • Question

  • Hi All,

    I want to email sopme files depending on the files size.

    I want to email the txt files in the folder if the files size is less than 20 MB. Otherwise I want to zip it.

    Can anyone tell me how to do this pls?


    Wednesday, January 5, 2011 4:49 AM


  • Your task can be broken down into 3 phases

    1. Finding if the file is > 20Mb

    2. Zip if >20

    3. Send Email.


    Use a Foreach loop to iterate over the file in the folder one by one and get Full path along with  file name of the current file in a Variable.

    1. Use Script task and below script to find if file is greater then 20MB

                long size;
                long SizeInMb;
                String VarFilepath = Dts.Variables["FilePath"].Value.ToString();
                System.IO.FileInfo info = new System.IO.FileInfo("c:\\t.sql");

                // will give size in bytes
                size= info.Length;

                //Convert in mb
                SizeInMb = size / 1048576;
                if (SizeInMb > 20)
                    //set a flag to zip
                    //set flag to unzip



    2. Now based on Flag to zip or unzip, you can use 7-zip or any other tool to zip it using Execute process task ( would need precedence constraints to check the flag)


    3. Now you can use Send Mail Task to attach the file and send it


    Rahul Kumar, MCTS, India, http://sqlserversolutions.blogspot.com/
    Wednesday, January 5, 2011 6:13 AM