Esempio n. 1
0
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            ExternalListFileRenamerStruct inputs = (ExternalListFileRenamerStruct)e.Argument;

            try
            {
                RenameFilesFromExternalList(inputs);
            }
            catch (Exception _ex)
            {
                this.progressStruct.Clear();
                this.progressStruct.ErrorMessage = String.Format("Error renaming files: {0}{1}", _ex.Message, Environment.NewLine);
                ReportProgress(0, this.progressStruct);
            }
        }
Esempio n. 2
0
        private void RenameFilesFromExternalList(ExternalListFileRenamerStruct inputValues)
        {
            string fileMask = null;

            string[] sourceFiles;
            int      sourceFileCount;

            long listFileLength;
            long fileNameOffset;

            StringBuilder renameScript;
            StringBuilder undoScript;

            string sourceFile;

            byte[] destinationFileTerminator = new byte[0];
            long   destinationFileBytesSize  = -1;

            byte[] destinationFileBytes;
            string destinationFile;
            string destinationFolder;

            long currentOffset;

            // verify directory exists and list file exists
            if (Directory.Exists(inputValues.SourceFolder) &&
                File.Exists(inputValues.ListFileLocation))
            {
                // set file mask
                fileMask = inputValues.SourceFileMask.Trim();
                fileMask = String.IsNullOrEmpty(fileMask) ? "*.*" : fileMask;

                // get source file count
                sourceFiles     = Directory.GetFiles(inputValues.SourceFolder, fileMask, SearchOption.TopDirectoryOnly);
                sourceFileCount = sourceFiles.GetLength(0);

                // verify some source files exist
                if (sourceFileCount == 0)
                {
                    throw new Exception(String.Format("Source directory is empty."));
                }
                else
                {
                    // convert file name offset to long
                    fileNameOffset = ByteConversion.GetLongValueFromString(inputValues.OffsetToFileNames);

                    // open List File
                    using (FileStream listFs = File.Open(inputValues.ListFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        listFileLength = listFs.Length;

                        // verify offset exists within list file
                        if (listFileLength < fileNameOffset)
                        {
                            throw new IOException(String.Format("File name offset is greater than the size of the list file."));
                        }
                        else
                        {
                            renameScript = new StringBuilder();
                            undoScript   = new StringBuilder();

                            // parse file name terminator, if included
                            if (inputValues.FileNameHasTerminator)
                            {
                                try
                                {
                                    destinationFileTerminator = ByteConversion.GetBytesFromHexString(inputValues.FileNameTerminator);
                                }
                                catch (Exception ex1)
                                {
                                    throw new FormatException(String.Format("Unable to convert File Name Terminator Bytes to hex: '{0}'", ex1.Message), ex1);
                                }
                            }
                            else // convert static filename size
                            {
                                try
                                {
                                    destinationFileBytesSize = ByteConversion.GetLongValueFromString(inputValues.FileNameStaticSize);
                                }
                                catch (Exception ex2)
                                {
                                    throw new FormatException(String.Format("Unable to convert File Name static size fo a number: '{0}'", ex2.Message), ex2);
                                }
                            }


                            try
                            {
                                // rename files
                                currentOffset = fileNameOffset;

                                for (int i = 0; i < sourceFileCount; i++)
                                {
                                    sourceFile = sourceFiles[i];

                                    // get name for files with a terminator
                                    if (inputValues.FileNameHasTerminator)
                                    {
                                        destinationFileBytesSize = ParseFile.GetNextOffset(listFs, currentOffset, destinationFileTerminator) - currentOffset;
                                    }

                                    // read destination file name
                                    if (destinationFileBytesSize > 0)
                                    {
                                        destinationFileBytes = ParseFile.ParseSimpleOffset(listFs, currentOffset, (int)destinationFileBytesSize);
                                        destinationFile      = this.getFileNameFromEncodedBytes(inputValues.FileNameEncoding, inputValues.FileNameCodePage, destinationFileBytes);
                                        currentOffset       += destinationFileBytesSize + destinationFileTerminator.Length;
                                    }
                                    else
                                    {
                                        this.progressStruct.Clear();
                                        this.progressStruct.FileName       = sourceFile;
                                        this.progressStruct.GenericMessage = String.Format("Warning: End of List File reached when renaming <{0}>{1}", sourceFile, Environment.NewLine);
                                        this.ReportProgress(Constants.ProgressMessageOnly, progressStruct);
                                        break;
                                    }

                                    // rename file and build scripts
                                    destinationFile = Path.Combine(inputValues.SourceFolder, destinationFile).Trim();

                                    if (inputValues.KeepOriginalFileExtension)
                                    {
                                        if (String.IsNullOrEmpty(Path.GetExtension(destinationFile)))
                                        {
                                            destinationFile += Path.GetExtension(sourceFile);
                                        }
                                        else
                                        {
                                            Path.ChangeExtension(destinationFile, Path.GetExtension(sourceFile));
                                        }
                                    }

                                    if (sourceFile != destinationFile)
                                    {
                                        renameScript.AppendFormat("rename \"{0}\" \"{1}\" {2}", Path.GetFileName(sourceFile), Path.GetFileName(destinationFile), Environment.NewLine);
                                        undoScript.AppendFormat("rename \"{0}\" \"{1}\" {2}", Path.GetFileName(destinationFile), Path.GetFileName(sourceFile), Environment.NewLine);

                                        destinationFolder = Path.GetDirectoryName(destinationFile);

                                        if (!Directory.Exists(destinationFolder))
                                        {
                                            Directory.CreateDirectory(destinationFolder);
                                        }

                                        File.Move(sourceFile, destinationFile);
                                    }



                                    // report progress
                                    this.progressStruct.Clear();
                                    this.progressStruct.FileName = sourceFile;
                                    this.ReportProgress(((i + 1) * 100) / sourceFileCount, progressStruct);
                                }
                            }
                            finally
                            {
                                if (renameScript.Length > 0)
                                {
                                    // write to file.
                                    FileUtil.CreateFileFromString(Path.Combine(inputValues.SourceFolder, RENAME_SCRIPT_NAME), renameScript.ToString());
                                }

                                if (undoScript.Length > 0)
                                {
                                    // write to file.
                                    FileUtil.CreateFileFromString(Path.Combine(inputValues.SourceFolder, UNDO_RENAME_SCRIPT_NAME), undoScript.ToString());
                                }
                            }
                        } // if (listFileLength < fileNameOffset)
                    }     // using (FileStream listFs = File.Open(inputValues.ListFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read))
                }         // if (sourceFileCount == 0)
            }
            else
            {
                throw new IOException(String.Format("Directory not found: <{0}>", inputValues.SourceFolder));
            } // if (Directory.Exists(inputValues.SourceFolder))
        }