Пример #1
0
        static void GetDirecotrySizeFromTextFileList(string textfilewithids, MigrationSettigs d, LogCsvFile log)
        {
            long count = 1;

            log.WriteInfo("[opening file]");
            string[] lines = System.IO.File.ReadAllLines(textfilewithids);
            log.WriteInfo(string.Format("[{0} records found]", lines.Length.ToString()));
            string folder = string.Empty;

            foreach (string idpair in lines)
            {
                try
                {
                    string[] values = idpair.Split(new char[] { ',' });
                    string   id     = values[0];

                    log.WriteInfo(string.Format("[processing {0}/{1}]", count.ToString(), lines.Length.ToString()));
                    folder = string.Format(d.SearchPattern, id);
                    long size = DirSize(folder, true, d, log);
                    log.WriteToCVS(folder, size.ToString());
                    log.WriteInfo("[done]");
                }

                catch (Exception ex)
                {
                    log.WriteToCVS(folder, "error");
                    log.WriteError("[" + folder + "]: " + ex.Message);
                }
                finally
                {
                    count++;
                }
            }
        }
Пример #2
0
        public static void GetMissingDirectories(string masterfile_path, string doTransit)
        {
            MigrationSettigs d   = Init(doTransit, "");
            LogCsvFile       log = new LogCsvFile("Directory, Exists", "missing_directories");

            string[] lines = System.IO.File.ReadAllLines(masterfile_path);

            foreach (string id in lines)
            {
                try
                {
                    string folderPath = string.Format(d.SearchPattern, id);
                    if (Directory.Exists(folderPath))
                    {
                        log.WriteToCVS(folderPath, "TRUE");
                    }
                    else
                    {
                        log.WriteToCVS(string.Format(d.SearchPattern, id), "FALSE");
                    }
                }
                catch (Exception ex)
                {
                    log.WriteToCVS(string.Format(d.SearchPattern, id), "ERROR");
                    log.WriteError(id);
                    log.WriteError(ex.Message);
                }
            }
        }
Пример #3
0
        public void ProcessJobs()
        {
            var jobs = Directory.GetFiles(job_repository, "*.ps1");

            foreach (string ps in jobs)
            {
                currentPs = ps.Substring(ps.LastIndexOf("\\") + 1);
                log.WriteInfo(string.Format("[initializing]: {0}", currentPs));
                log.WriteToCVS(Environment.MachineName, currentPs, "Initializing", DateTime.Now.ToLongTimeString(), DateTime.Today.ToShortDateString(), "");
                RunJobAsPs(ps);
            }
        }
Пример #4
0
        public static void ProcessFileValidations(string parent, string x, MigrationSettigs d, LogCsvFile log)
        {
            log.Header = "Folder,File,Length,ErrorType,Owner";

            try
            {  //# % * : < > ? / \ |
                if (x.IndexOf("~") != -1 ||
                    x.IndexOf("#") != -1 ||
                    x.IndexOf("%") != -1 ||
                    x.IndexOf("*") != -1 ||
                    x.IndexOf("<") != -1 ||
                    x.IndexOf(">") != -1 ||
                    x.IndexOf("?") != -1 ||
                    x.IndexOf("|") != -1)
                {
                    log.WriteToCVS(parent, x, x.Length.ToString(), "Invalid Char", LogManager.GetFileOwner(x));
                }

                if (x.Length > 260)
                {
                    log.WriteToCVS(parent, x, x.Length.ToString(), "URL Length", LogManager.GetFileOwner(x));
                }
                else
                {
                    string[] parts = x.Split(new char[] { '\\' });
                    foreach (string part in parts)
                    {
                        if (part.Length > 128)
                        {
                            log.WriteToCVS(parent, x, x.Length.ToString(), "Name Length", LogManager.GetFileOwner(x));
                            break;
                        }

                        if ((part.IndexOf(".") == -1) &&
                            (part.EndsWith("_file", StringComparison.CurrentCultureIgnoreCase) ||
                             part.EndsWith("_files", StringComparison.CurrentCultureIgnoreCase) ||
                             part.EndsWith("-filer", StringComparison.CurrentCultureIgnoreCase) ||
                             part.EndsWith("_fails", StringComparison.CurrentCultureIgnoreCase)))
                        {
                            log.WriteToCVS(parent, x, x.Length.ToString(), "Invalid Name", LogManager.GetFileOwner(x));
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteToCVS(parent, x, ex.Message, "ERROR", LogManager.GetFileOwner(x));
            }
        }
Пример #5
0
        public static void ProcessFileInfo(string parent, string x, MigrationSettigs d, LogCsvFile log)
        {
            try
            {
                string i        = "blank";
                string end_part = x.Substring(x.LastIndexOf("\\"));
                if (end_part.LastIndexOf(".") != -1)
                {
                    i = end_part.Substring(end_part.LastIndexOf("."));
                }

                if (x.Length < 260)
                {
                    FileInfo f = new FileInfo(x);
                    if (d.DoIncremental)
                    {
                        if (f.LastWriteTime < DateTime.Parse(d.LastDate))
                        {
                            log.WriteWarning("[excluded]: " + x);
                            return;
                        }
                    }
                    log.WriteToCVS(parent, x, i, f.Length.ToString(), LogManager.GetFileOwner(x), f.LastWriteTime.ToShortDateString());
                }
                else
                {
                    log.WriteWarning("[invalid_length]: " + x);
                }
            }
            catch (Exception ex)
            {
                log.WriteError("[" + x + "]:" + ex.Message);
            }
        }
Пример #6
0
        public static void CheckXlsToXlsxConversion(string masterfile_path, string doTransit, string from_lastdate)
        {
            MigrationSettigs d   = FileManager.Init(doTransit, from_lastdate);
            LogCsvFile       log = new LogCsvFile("parent, file, comments", "checkxlstoxlsx");

            string[] lines = System.IO.File.ReadAllLines(masterfile_path);

            log.WriteInfo("[Started at]: " + DateTime.Now.ToString());
            log.WriteInfo("[Parent Folders Found]: " + lines.Count().ToString());

            int convertedCount = 0;

            foreach (string idpair in lines)
            {
                string[] values = idpair.Split(new char[] { ',' });
                string   id     = values[0];

                string folderPath       = string.Format(d.SearchPattern, id);
                string parentFolderName = folderPath.Substring(folderPath.LastIndexOf("\\") + 1);
                var    files            = Directory.EnumerateFiles(folderPath, "*xls", SearchOption.AllDirectories);

                log.WriteInfo("[processing]: " + parentFolderName + "...");

                int fileCount = 0;
                foreach (string x in files)
                {
                    try
                    {
                        if (x.Substring(x.LastIndexOf(".")).ToLower() == ".xls")
                        {
                            string converted    = "false";
                            string xlsxFilePath = x.Replace(".xls", ".xlsx");
                            string xlsmFilePath = x.Replace(".xls", ".xlsm");

                            if (File.Exists(xlsxFilePath) || File.Exists(xlsmFilePath))
                            {
                                converted = "true";
                                convertedCount++;
                            }

                            fileCount++;

                            log.WriteToCVS(parentFolderName, x, converted);
                            log.WriteInfo("[" + x + "]" + " ...done ");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.WriteInfo(x);
                        log.WriteInfo(ex.ToString());
                    }
                }
            }

            log.WriteInfo("[xlsx files found]: " + convertedCount.ToString());
            log.WriteInfo("[Finished at]: " + DateTime.Now.ToString());
        }
Пример #7
0
 public static void ProcessFileChanges(string parent, string x, MigrationSettigs d, LogCsvFile log)
 {
     if (d.DoIncremental)
     {
         DateTime from_date = DateTime.Parse(d.LastDate);
         FileInfo file      = new FileInfo(x);
         if (file.LastWriteTime >= from_date)
         {
             log.WriteToCVS(parent, x, "object changed");
         }
     }
 }
Пример #8
0
        public static void ProcessDeleteXlsxFiles(string parent, string x, MigrationSettigs d, LogCsvFile log)
        {
            if (x.Length < 260)
            {
                FileInfo file = new FileInfo(x);
                if (file.Extension == "xlsx")
                {
                    file.Delete();
                }

                log.WriteInfo("[deleting]: " + x);
                log.WriteToCVS(parent, x, "deleted");
            }
            else
            {
                if (x.EndsWith(".xlsx"))
                {
                    File.Delete(x);
                }

                log.WriteWarning("[invalid_length]: " + x);
                log.WriteToCVS(parent, x, "invalid url");
            }
        }
Пример #9
0
        public static void CheckInvalidFolders(string masterfile_path, string doTransit, string report_name)
        {
            MigrationSettigs d   = Init(doTransit, "");
            LogCsvFile       log = new LogCsvFile("Folder, Name", report_name);

            string[] excludedFolders = d.ExcludeFolders.Split(',');

            string[] lines = System.IO.File.ReadAllLines(masterfile_path);
            foreach (string idpair in lines)
            {
                string[] values = idpair.Split(new char[] { ',' });
                string   id     = values[0];

                try
                {
                    string folderPath = string.Format(d.SearchPattern, id);
                    string parent     = folderPath.Substring(folderPath.LastIndexOf("\\") + 1);
                    foreach (string folder in Directory.GetDirectories(folderPath, "*", SearchOption.AllDirectories))
                    {
                        DirectoryInfo dir   = new DirectoryInfo(folder);
                        var           found = from f in excludedFolders
                                              where f.Equals(dir.Name, StringComparison.CurrentCultureIgnoreCase)
                                              select f;

                        if (found.Count() > 0)
                        {
                            log.WriteToCVS(parent, folder);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.WriteError(id);
                    log.WriteError(ex.Message);
                }
            }
        }
Пример #10
0
        private static string CheckFileForPassword(ExcelObj.Application excelApp, string parentFolderPath, string inputFile, MigrationSettigs d, LogCsvFile log)
        {
            var    missing = System.Reflection.Missing.Value;
            string fname   = inputFile.Substring(inputFile.LastIndexOf("\\") + 1);
            string status  = Constants.NotProcessed;

            ExcelObj.Workbook excelWrkBk = null;

            try
            {
                //Console.WriteLine("[processing]: " + fname);
                //excelWrkBk = excelApp.Workbooks.Open(
                //    inputFile,
                //    0,
                //    true,
                //    missing,
                //    "124567890",
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing,
                //    missing);


                if (OfficePasswordHelper.IsPasswordProtected(inputFile))
                {
                    Console.WriteLine("password protected file found...");
                    log.WriteToCVS(parentFolderPath, inputFile, LogManager.GetFileOwner(inputFile));
                    status = Constants.PasswordFound;
                }
                else
                {
                    status = Constants.NotProcessed;
                }
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2146827284)
                {
                    Console.WriteLine("password protected file found...");
                    log.WriteToCVS(parentFolderPath, inputFile, LogManager.GetFileOwner(inputFile));
                    status = Constants.PasswordFound;
                }
                else
                {
                    log.WriteError(inputFile);
                    log.WriteError(ex.Message);
                }
            }
            finally
            {
                try
                {
                    if (excelWrkBk != null)
                    {
                        excelWrkBk.Close(false, missing, missing);
                    }
                }
                catch (Exception e)
                {
                    log.WriteError(inputFile);
                    log.WriteError(e.Message);
                }
            }

            return(status);
        }
Пример #11
0
        private static string ConvertFile(ExcelObj.Application excelApp, string parentFolderPath, string inputFile, MigrationSettigs d, LogCsvFile log)
        {
            var      xlsxNewFile = string.Empty;
            string   fname       = inputFile.Substring(inputFile.LastIndexOf("\\") + 1);
            string   status      = Constants.NotProcessed;
            FileInfo i           = new FileInfo(inputFile);

            ExcelObj.Workbook excelWrkBk = null;

            try
            {
                Console.WriteLine("processing: " + fname);
                if (FileHasBeenConverted(i) &&
                    string.IsNullOrEmpty(d.LastDate))
                {
                    return(Constants.AlreadyConverted);
                }

                excelWrkBk = excelApp.Workbooks.Open(
                    inputFile,
                    0,
                    Type.Missing,
                    Type.Missing,
                    "1234567890",
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing,
                    Type.Missing);


                ExcelObj.XlFileFormat format =
                    Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook;


                if (excelWrkBk.HasVBProject)
                {
                    format      = Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled;
                    xlsxNewFile = inputFile.Replace(".xls", ".xlsm");
                }
                else if (i.Extension == ".xls")
                {
                    format      = Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook;
                    xlsxNewFile = inputFile.Replace(".xls", ".xlsx");
                }
                else if (i.Extension == ".xlt")
                {
                    format      = Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook;
                    xlsxNewFile = inputFile.Replace(".xlt", "");
                    xlsxNewFile = xlsxNewFile + "_template.xlsx";
                }

                excelWrkBk.SaveAs(xlsxNewFile,
                                  format,
                                  Type.Missing,
                                  Type.Missing,
                                  Type.Missing,
                                  Type.Missing,
                                  ExcelObj.XlSaveAsAccessMode.xlNoChange,
                                  Type.Missing,
                                  Type.Missing,
                                  Type.Missing,
                                  Type.Missing,
                                  Type.Missing);

                status = Constants.Converted;

                log.WriteToCVS(parentFolderPath, inputFile, "converted");
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2146827284)
                {
                    log.WriteInfo("password protected file found...");
                    log.WriteToCVS(parentFolderPath, inputFile, "password protected");
                    status = Constants.PasswordFound;
                }
                else
                {
                    log.WriteToCVS(parentFolderPath, inputFile, "error");
                    status = Constants.NotProcessed;
                }

                log.WriteError(inputFile);
                log.WriteError(ex.Message);
            }
            finally
            {
                try
                {
                    Console.WriteLine("closing: " + fname);
                    if (excelWrkBk != null)
                    {
                        excelWrkBk.Close(false, Type.Missing, Type.Missing);
                    }
                }
                catch (Exception e)
                {
                    log.WriteError(inputFile);
                    log.WriteError(e.Message);
                }
            }

            return(status);
        }