// Deletes files of a given filetype from a given directory. public void startDelete(string sourceDirectory, string filetype) { Operation_is_running opDelete = new Operation_is_running(); opDelete.Name = "opDelete" + deleteOpsRunning; deleteOpsRunning++; opDelete.Open("Delete", $"Deleting all {filetype} files from {sourceDirectory}, please wait"); Thread threadDelete = new Thread(() => { int filesDeleted = 0; DirectoryInfo sourceDir = new DirectoryInfo(@sourceDirectory); FileInfo[] filesToDelete = sourceDir.GetFiles("*" + filetype); try { foreach (FileInfo fileToDelete in filesToDelete) { File.Delete(sourceDirectory + fileToDelete.ToString()); filesDeleted++; opDelete.UpdateProgress(filesDeleted, filesToDelete.Length); } opDelete.UpdateText("Done!"); MessageBox.Show($"Successfully deleted {filesDeleted} files"); deleteOpsRunning--; opDelete.Exit(); } catch (DirectoryNotFoundException) { MessageBox.Show("Error: Directory not found"); return; } }); opDelete.Dispatcher.BeginInvoke(new Action(() => threadDelete.Start())); }
// Creates a given number of files of a given size and filetype in a given directory. public void startCreateFiles(string directory, string filetype, string numOfFiles, string sizeInMB) { Operation_is_running opCreate = new Operation_is_running(); opCreate.Name = "opCreate" + createFilesOpsRunning; createFilesOpsRunning++; opCreate.Open("Create", $"Creating {numOfFiles} {sizeInMB}MB {filetype} files in {directory}, please wait"); Thread threadCreateFiles = new Thread(() => { int sizeInBytes = Int32.Parse(sizeInMB) * 1000000; int filesMade = 0; try { for (int i = 0; i < Int32.Parse(numOfFiles); i++) { File.WriteAllBytes(directory + "file" + i.ToString() + filetype, new byte[sizeInBytes]); filesMade++; opCreate.UpdateProgress(filesMade, Int32.Parse(numOfFiles)); } opCreate.UpdateText("Done!"); MessageBox.Show($"Successfully made {filesMade} {sizeInMB}MB {filetype} files in {directory}"); createFilesOpsRunning--; opCreate.Exit(); } catch (DirectoryNotFoundException) { MessageBox.Show("Error: Directory not found"); return; } }); opCreate.Dispatcher.BeginInvoke(new Action(() => threadCreateFiles.Start())); }
// Automagically sorts pictures, documents, videos, and audio from a given source directory into a given destination directory. public void startSort(string sourceDirectory, string destDirectory) { // Look into Dictionary for optimization // https://stackoverflow.com/questions/24917532/can-you-create-variables-in-a-loop-c-sharp // https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2 Operation_is_running opSort = new Operation_is_running(); opSort.Name = "opSort" + sortOpsRunning; opSort.Open("Sort", "Sorting files, please wait"); Thread threadSort = new Thread(() => { // Ints that will be used to track progress int filesSorted = 0; int filesToSort = 0; // Create filetype arrays for each category string[] pictureFiletypes = { ".jpg", ".jpeg", ".png", ".gif", ".tiff" }; string[] documentFiletypes = { ".txt", ".doc", ".docx", ".xml", ".xlsx", ".pdf" }; string[] videoFiletypes = { ".mp4", ".mov", ".wmv", ".avi" }; string[] audioFiletypes = { ".mp3", ".wav", ".aac" }; // Directory declaration and creation string picDir = destDirectory + "Pictures\\"; string docDir = destDirectory + "Documents\\"; string vidDir = destDirectory + "Videos\\"; string audDir = destDirectory + "Audio\\"; Directory.CreateDirectory(picDir); Directory.CreateDirectory(docDir); Directory.CreateDirectory(vidDir); Directory.CreateDirectory(audDir); DirectoryInfo sourceDir = new DirectoryInfo(@sourceDirectory); List <FileInfo> picturesToSort = new List <FileInfo>(); List <FileInfo> documentsToSort = new List <FileInfo>(); List <FileInfo> videosToSort = new List <FileInfo>(); List <FileInfo> audioToSort = new List <FileInfo>(); try { // Gather pictures to move for (int i = 0; i < pictureFiletypes.Length; i++) { FileInfo[] filesToAdd = sourceDir.GetFiles("*" + pictureFiletypes[i]); foreach (FileInfo file in filesToAdd) { picturesToSort.Add(file); filesToSort++; } } // Gather documents to move for (int i = 0; i < documentFiletypes.Length; i++) { FileInfo[] filesToAdd = sourceDir.GetFiles("*" + documentFiletypes[i]); foreach (FileInfo file in filesToAdd) { documentsToSort.Add(file); filesToSort++; } } // Gather videos to move for (int i = 0; i < videoFiletypes.Length; i++) { FileInfo[] filesToAdd = sourceDir.GetFiles("*" + videoFiletypes[i]); foreach (FileInfo file in filesToAdd) { videosToSort.Add(file); filesToSort++; } } // Gather audio files to move for (int i = 0; i < audioFiletypes.Length; i++) { FileInfo[] filesToAdd = sourceDir.GetFiles("*" + audioFiletypes[i]); foreach (FileInfo file in filesToAdd) { audioToSort.Add(file); filesToSort++; } } // Finish gathering file list(s): begin actual sorting // Sort pictures foreach (FileInfo picToSort in picturesToSort) { File.Move(sourceDir + picToSort.ToString(), picDir + picToSort.ToString()); filesSorted++; opSort.UpdateProgress(filesSorted, filesToSort); } // Sort documents foreach (FileInfo docToSort in documentsToSort) { File.Move(sourceDir + docToSort.ToString(), docDir + docToSort.ToString()); filesSorted++; opSort.UpdateProgress(filesSorted, filesToSort); } // Sort videos foreach (FileInfo vidToSort in videosToSort) { File.Move(sourceDir + vidToSort.ToString(), vidDir + vidToSort.ToString()); filesSorted++; opSort.UpdateProgress(filesSorted, filesToSort); } // Sort audio foreach (FileInfo audToSort in audioToSort) { File.Move(sourceDir + audToSort.ToString(), audDir + audToSort.ToString()); filesSorted++; opSort.UpdateProgress(filesSorted, filesToSort); } opSort.UpdateText("Done!"); MessageBox.Show($"Successfully sorted {filesSorted} files!"); sortOpsRunning--; opSort.Exit(); } catch (DirectoryNotFoundException) { MessageBox.Show("Error: Directory not found"); return; } }); opSort.Dispatcher.BeginInvoke(new Action(() => threadSort.Start())); ; }