Beispiel #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            ErrorFlag = false;

            if (chkDocx.Checked == false)
            {
                MessageBox.Show("Please select at least one file type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(txtInputFolder.Text))
            {
                MessageBox.Show("Invalid Input Folder : " + txtInputFolder.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtFindWhat.Text == "")
            {
                MessageBox.Show("Empty Find Text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ReplaceTextOptions replaceTextOptions = new ReplaceTextOptions
            {
                docx = chkDocx.Checked,
                xlsx = chkXlsx.Checked,
                pptx = chkPptx.Checked,
                txt  = chkTxt.Checked,
                html = chkHtml.Checked,

                IncludeSubdirectories = chkIncludeSubdirectories.Checked,
                SameAsInputFolder     = chkSameAsInputFolder.Checked,

                InputFolder  = txtInputFolder.Text,
                OutputFolder = txtOutputFolder.Text,

                FindWhat    = txtFindWhat.Text,
                ReplaceWith = txtReplaceWith.Text
            };

            Log.CreateEntry();
            Log.CreateEntry("#", RepeatTextCount: 100);
            Log.CreateEntry("ReplaceText Operation Started");
            Log.CreateEntry("#", RepeatTextCount: 100);
            Log.CreateEntry();

            if (chkSameAsInputFolder.Checked == false)
            {
                if (txtOutputFolder.Text == "")
                {
                    MessageBox.Show("Output Folder Path is Empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                CopyFilesOptions copyFilesOptions = new CopyFilesOptions(replaceTextOptions);
                CopyFiles        copyFiles        = new CopyFiles();

                //try
                //{
                copyFiles.Run(copyFilesOptions);
                //}
                //catch (Exception Ex)
                //{
                //    ErrorFlag = true;
                //    Log.CreateEntry("-", RepeatTextCount: 100);
                //    Log.CreateEntry("CopyFiles: " + Ex.Message, "ERR");
                //    Log.CreateEntry("-", RepeatTextCount: 100);
                //}

                if (ErrorFlag)
                {
                    MessageBox.Show("Error Occured in Copying Files. View Log File for more Information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                replaceTextOptions.InputFolder = replaceTextOptions.OutputFolder;
            }

            ReplaceText replaceText = new ReplaceText(replaceTextOptions);

            //try
            //{
            replaceText.Run();
            //}
            //catch(Exception Ex)
            //{
            //    ErrorFlag = true;

            //    Log.CreateEntry("-", RepeatTextCount: 100);
            //    Log.CreateEntry("Error: " + Ex.Message, "ERR");
            //    Log.CreateEntry("-", RepeatTextCount: 100);

            //    MessageBox.Show("Error : " + Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}

            if (ErrorFlag)
            {
                Log.CreateEntry();
                Log.CreateEntry("#", RepeatTextCount: 100);
                Log.CreateEntry("Replace Text Operation Finished with Errors");
                Log.CreateEntry("#", RepeatTextCount: 100);
                Log.CreateEntry();

                MessageBox.Show("Replace Text Operation Finished with Errors. View Log File for more Information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Log.CreateEntry();
                Log.CreateEntry("#", RepeatTextCount: 100);
                Log.CreateEntry("Replace Text Operation is Complete");
                Log.CreateEntry("#", RepeatTextCount: 100);
                Log.CreateEntry();

                MessageBox.Show("Replace Text Operation is Complete", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Copy Files and Directories recursively from Source to Destination with Filters specified in CopyFileOptions
        /// </summary>
        /// <param name="copyFilesOptions">All copy file options</param>
        /// <returns>void</returns>
        public void Run(CopyFilesOptions copyFilesOptions)
        {
            if (copyFilesOptions.docx == false &&
                copyFilesOptions.xlsx == false &&
                copyFilesOptions.pptx == false &&
                copyFilesOptions.txt == false &&
                copyFilesOptions.html == false)
            {
                return;
            }

            if (copyFilesOptions.InputFolder == copyFilesOptions.OutputFolder)
            {
                return;
            }

            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(copyFilesOptions.OutputFolder) == false)
            {
                Directory.CreateDirectory(copyFilesOptions.OutputFolder);
            }

            IEnumerable <string> Files = null;

            if (copyFilesOptions.docx == true)
            {
                Files = Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.docx", SearchOption.TopDirectoryOnly);
            }

            if (copyFilesOptions.xlsx == true)
            {
                if (Files == null)
                {
                    Files = Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.xlsx", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    Files = Files.Concat(Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.xlsx", SearchOption.TopDirectoryOnly));
                }
            }

            if (copyFilesOptions.pptx == true)
            {
                if (Files == null)
                {
                    Files = Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.pptx", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    Files = Files.Concat(Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.pptx", SearchOption.TopDirectoryOnly));
                }
            }

            if (copyFilesOptions.txt == true)
            {
                if (Files == null)
                {
                    Files = Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.txt", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    Files = Files.Concat(Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.txt", SearchOption.TopDirectoryOnly));
                }
            }

            if (copyFilesOptions.html == true)
            {
                if (Files == null)
                {
                    Files = Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.html", SearchOption.TopDirectoryOnly);
                }
                else
                {
                    Files = Files.Concat(Directory.EnumerateFiles(copyFilesOptions.InputFolder, "*.html", SearchOption.TopDirectoryOnly));
                }
            }

            // Copy each file into it's new directory.
            foreach (string MyFile in Files)
            {
                FileInfo fi = new FileInfo(MyFile);

                try
                {
                    Log.CreateEntry("Copying File : " + Path.Combine(copyFilesOptions.OutputFolder, fi.Name), "INFO");
                    fi.CopyTo(Path.Combine(copyFilesOptions.OutputFolder, fi.Name), true);
                }
                catch (Exception Ex)
                {
                    ErrorFlag = true;

                    Log.CreateEntry("-", RepeatTextCount: 100);
                    Log.CreateEntry("CopyFiles : " + Ex.Message, "ERR");
                    Log.CreateEntry("-", RepeatTextCount: 100);
                }
            }

            //Copy each subdirectory using recursion.
            if (copyFilesOptions.IncludeSubdirectories == true)
            {
                DirectoryInfo InputDI  = new DirectoryInfo(copyFilesOptions.InputFolder);
                DirectoryInfo OutputDI = new DirectoryInfo(copyFilesOptions.OutputFolder);

                foreach (DirectoryInfo SubDI in InputDI.GetDirectories())
                {
                    try
                    {
                        DirectoryInfo NextSubDI =
                            OutputDI.CreateSubdirectory(SubDI.Name);

                        CopyFilesOptions SubDirCFO = new CopyFilesOptions(copyFilesOptions)
                        {
                            InputFolder  = SubDI.FullName,
                            OutputFolder = NextSubDI.FullName,
                        };

                        Run(SubDirCFO);
                    }
                    catch (Exception Ex)
                    {
                        ErrorFlag = true;

                        Log.CreateEntry("-", RepeatTextCount: 100);
                        Log.CreateEntry("CopyFiles: " + Ex.Message, "ERR");
                        Log.CreateEntry("-", RepeatTextCount: 100);
                    }
                }
            }
        }