using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Data;


namespace GetVideoFiles
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("This tool is designed for find which files include history vesion in all Site collections.");
            InitionFile();
            Console.WriteLine("Please input the web url:");
            String weburl = Console.ReadLine();
            Console.WriteLine("Please input Version num:");
            String getVerNumString = Console.ReadLine ();
            Int16 getVerNum=Convert.ToInt16(getVerNumString );

            Uri webAppUri = new Uri(weburl);
            SPWebApplication webApplication = SPWebApplication.Lookup(webAppUri);
            Console.WriteLine("WebUrl\tName\tUpdateTime\tModified By\tCreate By\tVerNum\tTotalSize");
            writer.WriteLine("WebUrl\tName\tUpdateTime\tModified By\tCreate By\tVerNum\tTotalSize");


            foreach (SPSite site in webApplication.Sites)
            {
                DataTable oDtRawData = null;
                oDtRawData = site.StorageManagementInformation(SPSite.StorageManagementInformationType.Document, SPSite.StorageManagementSortOrder.Decreasing, SPSite.StorageManagementSortedOn.Size, 400000);
                /*/// if you wan to see column names, loop through all the columns and find out the names and grab the needed one.
                 foreach (DataColumn oColumn in oDtRawData.Columns)
                         {
                              Console.WriteLine(oColumn.ColumnName);               
                           }
                 */


                // SPWeb web = site.OpenWeb();

                string siteUrl = site.Url;
                Int64 sizes = 0;
                Int64 historySizes = 0;
                Int64 totalSizes = 0;
                Int64 history = 0;
                foreach (DataRow oRow in oDtRawData.Rows)
                {

                    // Console.WriteLine("Processing......" + oRow["LeafName"].ToString() + "......" + oRow["WebUrl"].ToString());
                    string historySize2 = oRow["SizeOfVersions"].ToString();
                    history = Convert.ToInt64(historySize2);
                    if (history != 0)
                    {
                        string size1 = oRow["Size"].ToString();
                        string historySize1 = oRow["SizeOfVersions"].ToString();
                        string totalSize1 = oRow["TotalSize"].ToString();
                        Int64 size = Convert.ToInt64(size1);
                        Int64 historySize = Convert.ToInt64(historySize1);
                        Int64 totalSize = Convert.ToInt64(totalSize1);
                        sizes = sizes + size;
                        historySizes = historySizes + historySize;
                        totalSizes = totalSizes + totalSize;

                        string fullPath = "http://" + webAppUri.Host + "/" + oRow["Directory"].ToString() + "/" + oRow["LeafName"].ToString();

                        Console.WriteLine(fullPath);
                        SPFile file = null;
                        Int32 verNum = 0;
                       // string modifiedBy = null;
                        string modifiedByName = null;
                      //  string createBy = null;
                        string createByName = null;
                        using (SPWeb web = new SPSite(fullPath).OpenWeb())
                        {
                          //  web.AssociatedOwnerGroup
                            file = web.GetFile(fullPath);
                            verNum = file.Versions.Count;
                           
                           
                           // modifiedBy = file.ModifiedBy.Email.ToString ();
                          //  createBy = file.Author.Email.ToString();
                        
                            try
                            {
                                modifiedByName = file.Properties["vti_modifiedby"].ToString();
                                //if (file.ModifiedBy != null) modifiedByName = file.ModifiedBy.LoginName;
                            }
                            catch { }
                            try
                            {
                                createByName = file.Properties["vti_author"].ToString();
                                //if (file.Author != null) createByName = file.Author.LoginName;
                            }
                            catch { }
                           
                            //Console.WriteLine(file.Versions.Count + "" + web.Url);
                            //Console.WriteLine(file.TimeLastModified);
                        }
                        if (verNum >= getVerNum)
                        {
                            //Console.WriteLine(oRow["ModifiedDate"].ToString());
                            // Console.WriteLine(oRow["LeafName"].ToString() + "\t" + oRow["Size"].ToString() + "\t" + oRow["SizeOfVersions"].ToString() + "\t" + oRow["TotalSize"].ToString() + "\t" + oRow["WebUrl"].ToString());
                            writer.WriteLine(oRow["WebUrl"].ToString() + "\t" + oRow["LeafName"].ToString() + "\t" + oRow["ModifiedDate"].ToString() + "\t" + modifiedByName +"\t"+ createByName +"\t"+ verNum + "\t" + oRow["TotalSize"].ToString());
                        }
                    }

                }              

            }
           
            CloseFile();
            //Console.ReadKey();

        }


        static StreamWriter writer;
        static void InitionFile()
        {
            writer = new StreamWriter("files.txt", false);
        }
        static void CloseFile()
        {
            writer.Flush();
            writer.Close();
        }

        static void WriteLine(string msg)
        {
            writer.WriteLine(msg);
        }
    }
}