Exemple #1
0
        private void PreviewButton_Click(object sender, RoutedEventArgs e)
        {
            if (listObject.Count == 0)
            {
                MessageBox.Show("Chưa chọn tập tin hoặc thư mục");
                return;
            }
            if (listMethod.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn thao tác");
                return;
            }
            List <string> listExistItem = new List <string>();
            bool          existName     = false;

            foreach (var item in listObject)
            {
                listExistItem.Add(item.CurrName);
            }

            foreach (var item in listObject)
            {
                listExistItem.RemoveAt(0);
                string tmpItemName, extension = "";
                //nếu là file thì không thao tác với đuôi file
                if (isFileMode)
                {
                    int indexOfExtension = item.CurrName.LastIndexOf('.');
                    if (indexOfExtension >= 0)
                    {
                        extension   = item.CurrName.Substring(indexOfExtension);
                        tmpItemName = item.CurrName.Substring(0, indexOfExtension);
                    }
                    else
                    {
                        tmpItemName = item.CurrName;
                    }
                }
                else
                {
                    tmpItemName = item.CurrName;
                }
                foreach (var method in listMethod)
                {
                    // chỉ có replace là có thao tác với đuôi file
                    if (method.MethodName == "Replace")
                    {
                        string currString = method.MethodParameters[0];
                        string newString  = method.MethodParameters[1];
                        tmpItemName = XuLiChuoi.Replace(tmpItemName + extension, currString, newString);
                        if (isFileMode)
                        {
                            int indexOfExtension = tmpItemName.LastIndexOf('.');
                            if (indexOfExtension >= 0)
                            {
                                extension   = tmpItemName.Substring(indexOfExtension);
                                tmpItemName = tmpItemName.Substring(0, indexOfExtension);
                            }
                        }
                    }
                    else if (method.MethodName == "NewCase")
                    {
                        if (method.MethodParameters[0] == "True")
                        {
                            tmpItemName = XuLiChuoi.ToUpper(tmpItemName);
                        }
                        if (method.MethodParameters[1] == "True")
                        {
                            tmpItemName = XuLiChuoi.ToLower(tmpItemName);
                        }
                        if (method.MethodParameters[2] == "True")
                        {
                            tmpItemName = XuLiChuoi.ConvertFirstLetterOfEachWordToUpper(tmpItemName);
                        }
                    }
                    else if (method.MethodName == "FullnameNormalize")
                    {
                        if (method.MethodParameters[0] == "True")
                        {
                            tmpItemName = XuLiChuoi.Trim(tmpItemName);
                        }
                        if (method.MethodParameters[1] == "True")
                        {
                            tmpItemName = XuLiChuoi.ConvertFirstLetterOfEachWordToUpper(tmpItemName);
                        }
                        if (method.MethodParameters[2] == "True")
                        {
                            tmpItemName = XuLiChuoi.AllowOnlyOneSpaceBetweenWords(tmpItemName);
                        }
                    }
                    else if (method.MethodName == "Move")
                    {
                        if (method.MethodParameters[1] == "True")
                        {
                            try
                            {
                                tmpItemName = XuLiChuoi.MoveISBNFromEndToFrontOfString(tmpItemName, int.Parse(method.MethodParameters[0]));
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                item.Error = "Không thể di chuyển isbn nên thao tác move không được thực hiện";
                            }
                        }
                        else
                        {
                            try
                            {
                                tmpItemName = XuLiChuoi.MoveISBNFromFrontToEndOfString(tmpItemName, int.Parse(method.MethodParameters[0]));
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                item.Error = "Không thể di chuyển isbn nên thao tác move không được thực hiện";
                            }
                        }
                    }
                    else if (method.MethodName == "Unique")
                    {
                        tmpItemName = XuLiChuoi.CreateUniqueString();
                    }
                }
                item.NewName = tmpItemName + extension;
                if (listExistItem.Contains(item.NewName))
                {
                    item.Error = "Item bị trùng tên";
                    existName  = true;
                }
            }
            if (existName)
            {
                MessageBoxResult existNameMessageBox = MessageBox.Show("Có 1 số item bị trùng tên sau khi đổi tên, bạn có muốn giữ nguyên tên ban đầu hay thêm hậu tố là số?\nẤn Cancel để giữ nguyên tên ban đầu\nẤn OK để thêm hậu tố là số", "Bị trùng tên!!", MessageBoxButton.OKCancel);
                if (existNameMessageBox == MessageBoxResult.OK)
                {
                    isChangeExistName = true;
                }
                else
                {
                    isChangeExistName = false;
                }
            }
            else
            {
                MessageBox.Show("Tên các item sau khi đổi hoàn toàn hợp lệ");
            }
        }
Exemple #2
0
        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            if (listObject.Count == 0)
            {
                MessageBox.Show("Chưa chọn tập tin hoặc thư mục");
                return;
            }
            if (listMethod.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn thao tác");
                return;
            }
            List <string> listExistItem = new List <string>();
            bool          existName     = false;

            foreach (var item in listObject)
            {
                listExistItem.Add(item.CurrName);
            }

            foreach (var item in listObject)
            {
                listExistItem.RemoveAt(0);
                string tmpItemName, extension = "";
                //nếu là file thì không thao tác với đuôi file
                if (isFileMode)
                {
                    int indexOfExtension = item.CurrName.LastIndexOf('.');
                    if (indexOfExtension >= 0)
                    {
                        extension   = item.CurrName.Substring(indexOfExtension);
                        tmpItemName = item.CurrName.Substring(0, indexOfExtension);
                    }
                    else
                    {
                        tmpItemName = item.CurrName;
                    }
                }
                else
                {
                    tmpItemName = item.CurrName;
                }
                foreach (var method in listMethod)
                {
                    // chỉ có replace là có thao tác với đuôi file
                    if (method.MethodName == "Replace")
                    {
                        string currString = method.MethodParameters[0];
                        string newString  = method.MethodParameters[1];
                        tmpItemName = XuLiChuoi.Replace(tmpItemName + extension, currString, newString);
                        if (isFileMode)
                        {
                            int indexOfExtension = tmpItemName.LastIndexOf('.');
                            if (indexOfExtension >= 0)
                            {
                                extension   = tmpItemName.Substring(indexOfExtension);
                                tmpItemName = tmpItemName.Substring(0, indexOfExtension);
                            }
                        }
                    }
                    else if (method.MethodName == "NewCase")
                    {
                        if (method.MethodParameters[0] == "True")
                        {
                            tmpItemName = XuLiChuoi.ToUpper(tmpItemName);
                        }
                        if (method.MethodParameters[1] == "True")
                        {
                            tmpItemName = XuLiChuoi.ToLower(tmpItemName);
                        }
                        if (method.MethodParameters[2] == "True")
                        {
                            tmpItemName = XuLiChuoi.ConvertFirstLetterOfEachWordToUpper(tmpItemName);
                        }
                    }
                    else if (method.MethodName == "FullnameNormalize")
                    {
                        if (method.MethodParameters[0] == "True")
                        {
                            tmpItemName = XuLiChuoi.Trim(tmpItemName);
                        }
                        if (method.MethodParameters[1] == "True")
                        {
                            tmpItemName = XuLiChuoi.ConvertFirstLetterOfEachWordToUpper(tmpItemName);
                        }
                        if (method.MethodParameters[2] == "True")
                        {
                            tmpItemName = XuLiChuoi.AllowOnlyOneSpaceBetweenWords(tmpItemName);
                        }
                    }
                    else if (method.MethodName == "Move")
                    {
                        if (method.MethodParameters[1] == "True")
                        {
                            try
                            {
                                tmpItemName = XuLiChuoi.MoveISBNFromEndToFrontOfString(tmpItemName, int.Parse(method.MethodParameters[0]));
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                item.Error = "không thể di chuyển isbn nên thao tác move không được thực hiện";
                            }
                        }
                        else
                        {
                            try
                            {
                                tmpItemName = XuLiChuoi.MoveISBNFromFrontToEndOfString(tmpItemName, int.Parse(method.MethodParameters[0]));
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                item.Error = "không thể di chuyển isbn nên thao tác move không được thực hiện";
                            }
                        }
                    }
                    else if (method.MethodName == "Unique")
                    {
                        tmpItemName = XuLiChuoi.CreateUniqueString();
                    }
                }

                //chưa cấu hình
                item.NewName = tmpItemName + extension;
                if (listExistItem.Contains(item.NewName))
                {
                    item.Error = "Item bị trùng tên";
                    existName  = true;
                }
                else
                {
                    string newPath = item.Path.Substring(0, item.Path.LastIndexOf('\\')) + '\\' + item.NewName;
                    if (isFileMode)
                    {
                        try
                        {
                            File.Move(item.Path, newPath);
                        }
                        catch (Exception)
                        {
                            item.Error = "File không còn tồn tại ở thời điểm này";
                        }
                    }
                    else
                    {
                        try
                        {
                            var dir = new DirectoryInfo(item.Path);
                            dir.MoveTo(item.Path + XuLiChuoi.CreateUniqueString()); // di chuyển đến thư mục tạm thời
                            dir.MoveTo(newPath);
                        }
                        catch (Exception)
                        {
                            item.Error = "Thư mục không còn tồn tại ở thời điểm này";
                        }
                    }
                }
            }
            if (isChangeExistName == null && existName)
            {
                MessageBoxResult existNameMessageBox = MessageBox.Show("Có 1 số item bị trùng tên sau khi đổi tên, bạn có muốn giữ nguyên tên ban đầu hay thêm hậu tố là số?\nẤn Cancel để giữ nguyên tên ban đầu\nẤn OK để thêm hậu tố là số", "Bị trùng tên!!", MessageBoxButton.OKCancel);
                if (existNameMessageBox == MessageBoxResult.OK)
                {
                    isChangeExistName = true;
                }
                else
                {
                    isChangeExistName = false;
                }
            }
            if (isChangeExistName == true)
            {
                foreach (var item in listObject)
                {
                    listExistItem.Add(item.CurrName);
                }
                foreach (var item in listObject)
                {
                    listExistItem.RemoveAt(0);
                    if (item.Error == "Item bị trùng tên")
                    {
                        int    count = 1;
                        string itemName, ext = "";
                        if (isFileMode)
                        {
                            itemName = item.NewName.Substring(0, item.NewName.LastIndexOf('.'));
                            ext      = item.NewName.Substring(item.NewName.LastIndexOf('.'));
                        }
                        else
                        {
                            itemName = item.NewName;
                        }
                        while (listExistItem.Contains(itemName + count.ToString() + ext))
                        {
                            count++;
                        }
                        if (isFileMode)
                        {
                            item.NewName = itemName + count.ToString() + ext;
                            string newPath = item.Path.Substring(0, item.Path.LastIndexOf('\\')) + '\\' + item.NewName;
                            try
                            {
                                File.Move(item.Path, newPath);
                                item.Error = "";
                            }
                            catch (Exception)
                            {
                                item.Error = "File không còn tồn tại ở thời điểm này";
                            }
                        }
                        else
                        {
                            item.NewName = itemName + count.ToString();
                            string newPath = item.Path.Substring(0, item.Path.LastIndexOf('\\')) + '\\' + item.NewName;
                            try
                            {
                                var dir = new DirectoryInfo(item.Path);
                                dir.MoveTo(item.Path + XuLiChuoi.CreateUniqueString()); // di chuyển đến thư mục tạm thời
                                dir.MoveTo(newPath);
                                item.Error = "";
                            }
                            catch (Exception)
                            {
                                item.Error = "Thư mục không còn tồn tại ở thời điểm này";
                            }
                        }
                    }
                }
            }
            else if (isChangeExistName == false)
            {
                foreach (var item in listObject)
                {
                    if (item.Error == "Item bị trùng tên")
                    {
                        item.NewName = item.CurrName;
                        item.Error   = "";
                    }
                }
            }
            isChangeExistName = null;
            MessageBox.Show("Đổi tên thành công");
            isRefreshLisstObject = true;
        }