/// <summary>
        /// Perform Batch Rename and return the result
        /// </summary>
        /// <param name="fileList">The list of file wanted to batch rename</param>
        /// <param name="operations">The list of String Operation wanted to perform on name list</param>
        /// <returns>A list of FIleObj</returns>
        public List <FileObj> BatchRename(List <FileObj> fileList, List <StringOperation> operations)
        {
            List <FileObj> result = new List <FileObj>(fileList);

            if (NewFileNames.Count != 0) // clear list to save new changed names
            {
                NewFileNames.Clear();
            }

            if (FileList.Count != 0)
            {
                FileList.Clear();
            }

            for (int i = 0; i < fileList.Count; i++)
            {
                string   path     = fileList[i].Path + "\\" + fileList[i].Name;
                FileInfo fileInfo = new FileInfo(path);
                FileList.Add(fileInfo);
                NewFileNames.Add(Path.GetFileNameWithoutExtension(fileList[i].Name));
            }



            for (int i = 0; i < operations.Count; i++)
            {
                for (int j = 0; j < NewFileNames.Count; j++)
                {
                    /*If the name is in error list, skip the rename process, to preserve the pre-error value*/
                    bool IsInErrorList = isInErrorList(j);
                    if (IsInErrorList)
                    {
                        continue;
                    }
                    try
                    {
                        NewFileNames[j] = operations[i].OperateString(NewFileNames[j]); // perform operation
                    }
                    catch (Exception e)                                                 //if operation has failed
                    {
                        BatchRenameError error = new BatchRenameError()
                        {
                            NameErrorIndex = j,               // save the position of the string which caused the error
                            LastNameValue  = NewFileNames[j], //save the last values of the string before error
                            Message        = e.Message,       //the error message
                        };
                        errors.Add(error);
                    }
                }
            }

            //attach file name with its file extension and error messages that goes along with it if there's one
            List <string> ErrorMessages = GetErrorList();

            for (int i = 0; i < fileList.Count; i++)
            {
                NewFileNames[i]  += FileList[i].Extension;
                result[i].NewName = NewFileNames[i];
                result[i].Error   = ErrorMessages[i];
            }

            //if handling fails or user refuses to change
            if (handleDuplicateFiles() == false)
            {
                return(result);
            }
            ;

            for (int i = 0; i < NewFileNames.Count; i++)
            {
                if (result[i].NewName != NewFileNames[i])
                {
                    result[i].NewName = NewFileNames[i];
                    result[i].Error   = "Name changed to avoid duplication";
                }
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Perform Batch Rename and return the result
        /// </summary>
        /// <param name="folderList">The list of folder wanted to batch rename</param>
        /// <param name="operations">The list of String Operation wanted to perform on name list</param>
        /// <returns>A list of FolderObj</returns>
        public List <FolderObj> BatchRename(List <FolderObj> folderList, List <StringOperation> operations)
        {
            List <FolderObj> result = new List <FolderObj>(folderList);

            if (NewFolderNames.Count != 0) // clear list to save new changed names
            {
                NewFolderNames.Clear();
            }

            if (FolderList.Count != 0)
            {
                FolderList.Clear();
            }

            for (int i = 0; i < folderList.Count; i++)
            {
                string        path          = folderList[i].Path + "\\" + folderList[i].Name;
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FolderList.Add(directoryInfo);
                NewFolderNames.Add(directoryInfo.Name);
                Debug.WriteLine(directoryInfo.Name);
            }



            for (int i = 0; i < operations.Count; i++)
            {
                for (int j = 0; j < NewFolderNames.Count; j++)
                {
                    /*If the name is in error list, skip the rename process, to preserve the pre-error value*/
                    bool IsInErrorList = isInErrorList(j);
                    if (IsInErrorList)
                    {
                        continue;
                    }
                    try
                    {
                        NewFolderNames[j] = operations[i].OperateString(NewFolderNames[j]); // perform operation
                    }
                    catch (Exception e)                                                     //if operation has failed
                    {
                        BatchRenameError error = new BatchRenameError()
                        {
                            NameErrorIndex = j,                 // save the position of the string which caused the error
                            LastNameValue  = NewFolderNames[j], //save the last values of the string before error
                            Message        = e.Message,         //the error message
                        };
                        errors.Add(error);
                    }
                }
            }

            //send back error messages that goes along with the folder name if there's one
            List <string> ErrorMessages = GetErrorList();

            for (int i = 0; i < folderList.Count; i++)
            {
                result[i].NewName = NewFolderNames[i];
                result[i].Error   = ErrorMessages[i];
            }

            //if handling fails or user refuses to change
            if (handleDuplicateFolder() == false)
            {
                return(result);
            }
            ;

            for (int i = 0; i < NewFolderNames.Count; i++)
            {
                if (result[i].NewName != NewFolderNames[i])
                {
                    result[i].NewName = NewFolderNames[i];
                    result[i].Error   = "Name changed to avoid duplication";
                }
            }

            return(result);
        }