Search code examples
c#pdffolderbrowserdialog

How do I list the file in it by increasing the filename by one?


I have thousands of project folders and inside each folder I have another folder named test. There is only one pdf file in this folder. When I select the top folder, I want to get the pdfs in these folders and copy them to another location. How can I select these pdfs?

There are other pdfs under the Project Folders, I only want the pdfs in the test folder.

My folder names are increasing one by one(Project1,Project2,3,4,5,6,7.....)

My Project
 ↳-Project1
     ↳Test
        ↳Project1.pdf
 ↳-Project2
     ↳Test
        ↳Project2.pdf
 ↳-Project3
     ↳Test
        ↳Project2.pdf

I want to something just like this

The image belongs to me. Currently it only lists all pdfs in selected folders. I couldn't filter pdf files. I am using FolderBrowserDialog for select the folder


Solution

  •     private string[] dirs;
        private string[] files;
        private System.IO.FileInfo file;
        private void GetPDF(string path, ref List<string> listPDF)
        {
            try
            {
                dirs = System.IO.Directory.GetDirectories(path);
                foreach (string item in dirs)
                {
                    GetPDF(item, ref listPDF);
                }
    
                files = System.IO.Directory.GetFiles(path);
                foreach (string item in files)
                {
                    file = new System.IO.FileInfo(item);
                    if (file.DirectoryName.ToLower().EndsWith("test") && file.Extension.ToLower() == ".pdf")
                    {
                        listPDF.Add(file.FullName);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    
        private void Function1()
        {
            try
            {
                System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    List<string> listPDF = new List<string>();
                    GetPDF(dialog.SelectedPath, ref listPDF);
                    listPDF.Sort();
                    foreach(string filePath in listPDF)
                    {
                        Console.WriteLine(filePath);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }