Esempio n. 1
0
        private void cboHandleMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string modeName = this.cboHandleMode.Text;

            FileRenameMode mode = (FileRenameMode)Enum.Parse(typeof(FileRenameMode), modeName);

            if (InfoHelper.HandleModeDescriptions.ContainsKey(mode))
            {
                this.lblModeDescription.Text = $"({ InfoHelper.HandleModeDescriptions[mode]})";
            }

            bool isInsertMode           = mode == FileRenameMode.Insert;
            bool isExtractMode          = mode == FileRenameMode.Extract;
            bool isTrimMode             = mode == FileRenameMode.Trim;
            bool isInsertFolderNameMode = mode == FileRenameMode.InsertFolderName;

            this.panelExtract.Enabled = isExtractMode;

            this.panelOldChar.Enabled = !isExtractMode && !isInsertMode && !isInsertFolderNameMode;
            this.panelNewChar.Enabled = !isExtractMode && !isTrimMode;

            this.txtNewChar.Text = isInsertFolderNameMode && !string.IsNullOrEmpty(this.txtFolder.Text) ? new DirectoryInfo(this.txtFolder.Text).Name : "";

            this.panelInsert.Enabled            = isInsertFolderNameMode || isInsertMode;
            this.chkOldCharByExpression.Enabled = mode == FileRenameMode.RemoveOrReplace;
        }
Esempio n. 2
0
        private void PreHandleFileName()
        {
            this.AddFiles();

            string oldChar = this.txtOldChar.Text;
            string newChar = this.txtNewChar.Text;

            FileRenameMode mode = (FileRenameMode)Enum.Parse(typeof(FileRenameMode), this.cboHandleMode.Text);

            if (mode == FileRenameMode.RemoveOrReplace || mode == FileRenameMode.Trim)
            {
                if (string.IsNullOrEmpty(oldChar))
                {
                    MessageBox.Show("The old char can't be empty.");
                    return;
                }
            }
            else if (mode == FileRenameMode.Insert)
            {
                if (string.IsNullOrEmpty(newChar))
                {
                    MessageBox.Show("The new char can't be empty.");
                    return;
                }
            }
            else if (mode == FileRenameMode.Extract)
            {
                int startPos = (int)this.nudStartPosition.Value;
                int endPos   = (int)this.nudEndPosition.Value;

                if (endPos >= 1 && startPos > endPos)
                {
                    MessageBox.Show("From position must be less than to position.");
                }
            }

            foreach (ListViewItem item in this.lvFile.Items)
            {
                FileInfo file = item.Tag as FileInfo;

                string newFileName = null;
                if (this.chkBaseOnNewName.Checked && !string.IsNullOrEmpty(item.SubItems[1].Tag?.ToString()))
                {
                    newFileName = item.SubItems[1].Tag.ToString();
                }

                FileRenameOption option = new FileRenameOption()
                {
                    Mode                      = mode,
                    DoRename                  = false,
                    ExcessiveFileName         = newFileName,
                    HandleOldCharByExpression = this.chkOldCharByExpression.Checked,
                    OldChar                   = oldChar,
                    NewChar                   = newChar,
                    InsertBeforeMarkChar      = this.txtInsertBefore.Text,
                    InsertAfterMarkChar       = this.txtInsertAfter.Text,
                    NewCharInsertBefore       = this.rbNewCharInsertBefore.Checked,
                    NewCharInsertAfter        = this.rbNewCharInsertAfter.Checked,
                    ExtractByPosition         = this.rbExtractByPosition.Checked,
                    ExtractByExpression       = this.rbExtractByExpression.Checked,
                    ExtractStartPosition      = (int)this.nudStartPosition.Value,
                    ExtractEndPosition        = (int)this.nudEndPosition.Value,
                    ExtractExpression         = this.txtExractExpression.Text,
                };

                FileRenameProcessor processor = new FileRenameProcessor(option);

                string errMsg = "";
                newFileName = processor.Rename(file, out errMsg);

                if (!string.IsNullOrEmpty(errMsg))
                {
                    MessageBox.Show(errMsg);
                    break;
                }
                else
                {
                    item.SubItems[1].Tag  = Path.GetFileNameWithoutExtension(newFileName);
                    item.SubItems[1].Text = newFileName;
                }

                item.ForeColor = file.Name == newFileName ? Color.Black : Color.Blue;
            }
        }
Esempio n. 3
0
        public string Rename(FileInfo file, out string errMsg)
        {
            errMsg = "";

            FileRenameMode mode = this.Option.Mode;

            string oldChar = this.Option.OldChar;
            string newChar = this.Option.NewChar;

            string fileName    = Path.GetFileNameWithoutExtension(file.Name);
            string newFileName = "";

            if (!string.IsNullOrEmpty(this.Option.ExcessiveFileName))
            {
                fileName = this.Option.ExcessiveFileName;
            }

            try
            {
                if (mode == FileRenameMode.RemoveOrReplace)
                {
                    if (!this.Option.HandleOldCharByExpression)
                    {
                        newFileName = fileName.Replace(oldChar, newChar);
                    }
                    else
                    {
                        if (this.CheckExpression(oldChar, out errMsg))
                        {
                            bool matched = false;

                            string result = this.HandleExpressioin(oldChar, fileName, out matched);

                            if (matched)
                            {
                                newFileName = fileName.Replace(result, newChar);
                            }
                            else
                            {
                                newFileName = result;
                            }
                        }
                        else
                        {
                            return(file.Name);
                        }
                    }
                }
                else if (mode == FileRenameMode.Trim)
                {
                    newFileName = fileName.Trim(oldChar.ToArray());
                }
                else if (mode == FileRenameMode.Insert || mode == FileRenameMode.InsertFolderName)
                {
                    string insertChar = newChar;

                    string insertBeforeChar = this.Option.InsertBeforeMarkChar;
                    string insertAfterChar  = this.Option.InsertAfterMarkChar;

                    int insertBeforeIndex = -1;
                    int insertAfterIndex  = -1;

                    if (!string.IsNullOrEmpty(insertBeforeChar))
                    {
                        insertBeforeIndex = fileName.IndexOf(insertBeforeChar);
                    }

                    if (!string.IsNullOrEmpty(insertAfterChar))
                    {
                        insertAfterIndex = fileName.IndexOf(insertAfterChar);
                    }

                    if (this.Option.NewCharInsertBefore)
                    {
                        if (insertBeforeIndex != -1)
                        {
                            newFileName = fileName.Substring(0, insertBeforeIndex) + newChar + fileName.Substring(insertBeforeIndex);
                        }
                        else
                        {
                            newFileName = newChar + fileName;
                        }
                    }
                    else
                    {
                        if (insertAfterIndex != -1)
                        {
                            newFileName = fileName.Substring(0, insertAfterIndex + insertAfterChar.Length) + newChar + fileName.Substring(insertAfterIndex + insertAfterChar.Length);
                        }
                        else
                        {
                            newFileName = fileName + insertChar;
                        }
                    }
                }
                else if (mode == FileRenameMode.Extract)
                {
                    if (this.Option.ExtractByPosition)
                    {
                        int startPos = this.Option.ExtractStartPosition;
                        int endPos   = this.Option.ExtractEndPosition;

                        if (startPos <= fileName.Length)
                        {
                            newFileName = fileName.Substring(startPos - 1, endPos <= 0 ? fileName.Length - startPos + 1 : endPos - startPos + 1);
                        }
                    }
                    else if (this.Option.ExtractByExpression)
                    {
                        string expression = this.Option.ExtractExpression;
                        if (this.CheckExpression(expression, out errMsg))
                        {
                            newFileName = this.HandleExpressioin(expression, fileName, out _);
                        }
                        else
                        {
                            return(file.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = $"Error occurs when handle \"{file.Name}\":{ex.Message}";
                return(file.Name);
            }

            newFileName += file.Extension;

            if (string.IsNullOrEmpty(errMsg) && !string.IsNullOrEmpty(newFileName) && newFileName != fileName && this.Option.DoRename)
            {
                string newFilePath = Path.Combine(file.DirectoryName, newFileName);
                if (!File.Exists(newFilePath))
                {
                    file.MoveTo(newFilePath);
                }
            }

            return(newFileName);
        }