Scan hard drive for PDF, Excel and Word files except hidden files/folders and copy them to one drive sync folder [closed]

2 days ago 1
ARTICLE AD BOX

App that scans hard drive for PDF, Excel and Word files except hidden files/folders and copies them to one drive sync folder

I have written this code but doesn't work, it goes on forever and doesn't copy all files and also doesn't exclude the exclude directories

I also don't know how to exclude the hidden folders/files it crashes when it goes to "documents and settings" for example any help?

Working on .NET 4.7.2

public Form1() { InitializeComponent(); label1.Text= "C:\\Users\\"+ Environment.UserName+ "\\OneDrive"; progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.MarqueeAnimationSpeed = 0; progressBar1.Visible = false; } private void btnStart_Click(object sender, EventArgs e) { btnStart.Enabled = false; btnStart.Text = "Copying to OneDrive.."; progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.MarqueeAnimationSpeed = 5; progressBar1.Visible = true; ///////////////////////////pdf///////////////////////////////////// /// var sourcePath = @"C:\\"; var targetPath = @"C:\\Users\\"+ Environment.UserName+ "\\OneDrive\\saved"; var extensions = new[] { ".pdf", ".docx", ".ppt", ".xls" }; var pathsToAvoid = new[] { @"c:\\windows\\", @"c:\\$RECYCLE.BIN\\", @"c:\\System Volume Information\\", @"C:\\Users\\"+ Environment.UserName+ "\\OneDrive\\" }; var files = System.IO.Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories) .Where(file => { var fullPath = System.IO.Path.GetFullPath(file); var ext = System.IO.Path.GetExtension(file); // skip files in pathsToAvoid if (pathsToAvoid.Any(p => fullPath.StartsWith(p, StringComparison.InvariantCultureIgnoreCase))) return false; // keep only desired extensions (case-insensitive) return extensions.Contains(ext, StringComparer.InvariantCultureIgnoreCase); }) .Select(file => new { Source = file, Destination = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(file)) }); try { foreach (var file in files) { var destDir = System.IO.Path.GetDirectoryName(file.Destination); if (!string.IsNullOrEmpty(destDir) && !Directory.Exists(destDir)) Directory.CreateDirectory(destDir); File.Copy(file.Source, file.Destination, true); // btnStart.Text = file.Source; } } catch (Exception ex) { // void method: use plain return, not 'return null' // MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (var file in files) { var destDir = System.IO.Path.GetDirectoryName(file.Destination); if (!string.IsNullOrEmpty(destDir) && !Directory.Exists(destDir)) Directory.CreateDirectory(destDir); File.Copy(file.Source, file.Destination, true); btnStart.Text = file.Source; } progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.MarqueeAnimationSpeed = 0; progressBar1.Visible = false; btnStart.Text = "Finished."; MessageBox.Show("All PDF, DOCX, PPT, XLS files have been copied to OneDrive.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); ///////////////////////////pdf///////////////////////////////////// /// // btnStart.Enabled = true; }
Read Entire Article