Exemple #1
0
        private void InitialiseWork(string srcDir, string dstDir)
        {
            DictTargetFolderToSrcFiles.Clear();
            FilesProcessed.Clear();
            FoldersCreated.Clear();

            DirectoryInfo info = new DirectoryInfo(srcDir);

            foreach (var file in info.GetFiles("*.*", SearchOption.AllDirectories))
            {
                string targetDir = FileOrganizerUtility.PrepareTargetDirFromFile(dstDir, file);

                if (!DictTargetFolderToSrcFiles.ContainsKey(targetDir))
                {
                    DictTargetFolderToSrcFiles.Add(targetDir, new List <FileInfo>()
                    {
                        file
                    });
                }
                else
                {
                    DictTargetFolderToSrcFiles[targetDir].Add(file);
                }
            }
        }
Exemple #2
0
        private void Revert(
            CancellationTokenSource tokenSrc,
            Action <string> UpdateStatus)
        {
            while (FilesProcessed.Count > 0)
            {
                PauseEvent.WaitOne();
                UpdateStatus($"Deleting file: {FilesProcessed[0]}");
                File.Delete(FilesProcessed[0]);
                FilesProcessed.RemoveAt(0);
            }

            while (FoldersCreated.Count > 0)
            {
                PauseEvent.WaitOne();
                UpdateStatus($"Deleting folder: {FoldersCreated[0]}");
                Directory.Delete(FoldersCreated[0], true);
                FoldersCreated.RemoveAt(0);
            }
        }
        protected virtual void OnFilesProcessed(int start, int length)
        {
            var processed = CreateReadOnlyList(start, length);

            FilesProcessed?.Invoke(this, new FileInfoReadOnlyListEventArgs(processed));
        }
Exemple #4
0
        private void Work(
            CancellationTokenSource tokenSrc,
            Action <string> UpdateStatus)
        {
            while (!tokenSrc.IsCancellationRequested)
            {
                PauseEvent.WaitOne();

                KeyValuePair <string, List <FileInfo> >?work = GetWork();
                if (work == null)
                {
                    //// task exiting. No more files to process.
                    return;
                }

                string          targetFolder = work.Value.Key;
                List <FileInfo> srcFiles     = work.Value.Value;

                if (!Directory.Exists(targetFolder))
                {
                    DirectoryInfo inf = new DirectoryInfo(targetFolder);
                    while (!inf.Parent.Exists)
                    {
                        inf = inf.Parent;
                    }

                    UpdateStatus?.Invoke($"Creating directory: {targetFolder}");
                    Directory.CreateDirectory(targetFolder);
                    if (!FoldersCreated.Contains(inf.FullName))
                    {
                        lock (_sync)
                        {
                            if (!FoldersCreated.Any(x => inf.FullName.Contains(x)))
                            {
                                FoldersCreated.Add(inf.FullName);
                            }
                        }
                    }
                }

                foreach (FileInfo srcFile in srcFiles)
                {
                    PauseEvent.WaitOne();
                    if (tokenSrc.IsCancellationRequested)
                    {
                        break;
                    }

                    UpdateStatus?.Invoke($"Processing: {srcFile.FullName}");
                    string dstFile = FileOrganizerUtility.GetTargetFileName(
                        srcFile.FullName,
                        targetFolder);

                    if (File.Exists(dstFile))
                    {
                        dstFile = FileOrganizerUtility.AddGuidToFileNameConflict(dstFile);
                    }

                    if (srcFile.CopyTo(dstFile).Exists != true)
                    {
                        throw new IOException(
                                  $"Failed to copy file from '{srcFile.FullName}' to '{dstFile}'");
                    }
                    FilesProcessed.Add(dstFile);
                    UpdateStatus?.Invoke($"Copied source: {srcFile.FullName} to destination: {dstFile}");
                }
            }
        }