Folder traversal and extraction

Posted by bandit8 on Fri, 17 Dec 2021 08:14:43 +0100

On the traversal of all files under a certain path, a simple example of subsequent copying or cutting is proposed

Purpose:

Find some files of specific types in the system, then select save in listbox, select an existing path in the system, and copy or cut the files to the path
1, Page setup.
(two textbox es, two button s and one listbox are required)

2 find the initial path to traverse the query and set the click event of button1

private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog s1 = new SaveFileDialog();
            s1.Title = "Traversal position";
            s1.Filter = "Traversal position(*.)|*." ;
            s1.FilterIndex = 1;
            s1.FileName = "Traverse from here";
            if (
                s1.ShowDialog() == DialogResult.OK)
            {
            }
            textBox1.Text = Path.GetDirectoryName(s1.FileName).ToString();   
            }

3 traverse from the initial position
Insert the file saved by the list and write a function to traverse

 public static List<string> Wenjian = new List<string>();
        public static void Director(string dir)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
            foreach (FileSystemInfo fsinfo in fsinfos)
            {
                if (fsinfo is DirectoryInfo)     //Determine whether it is a folder
                {
                    Director(fsinfo.FullName);//Recursive call
                }
                else
                {
                    Wenjian.Add(fsinfo.FullName);
                }
            }
        }

4. Insert the path saved in wenjian into listbox, and insert the following code in the click event of button1.

            Director(textBox1.Text);
            for(int a=0;a<Wenjian.Count;a++)
            {
                listBox1.Items.Add(Wenjian[a]);
            }

5 select the path of the saved file
Set the click event of button2 as follows:

 private void button2_Click(object sender, EventArgs e)
            {
            SaveFileDialog s1 = new SaveFileDialog();
            s1.Title = "Save file location";
            s1.Filter = "Location file(*.)|*.";
            s1.FilterIndex = 1;
            s1.FileName = "Store here";
            if (
                s1.ShowDialog() == DialogResult.OK)
            {
            }
            textBox2.Text = Path.GetDirectoryName(s1.FileName).ToString();
            }

6. Copy or cut the file with the path saved in listbox
Create a function to copy or cut:

 public void CopyToFileTianhouzhui(string sourceName, string folderPath)
        {
            /* Suppose sourceName is D:\try \ try txt
               Path.GetFileNameWithoutExtension(sourceName);Get try
                Path.GetExtension(sourceName);Get txt
                Path.GetFileName(sourceName);Get a try txt
                Path.GetDirectoryName(sourceName); Get D:\try
                string newPath = Path.ChangeExtension(sourceName, "doc");//Change the file extension to Doc, i.e. D:\try \ try doc
            */
            string fileName = Path.GetFileName(sourceName);
            //Target overall path
            string targetPath = Path.Combine(folderPath, fileName);
            //If the current file exists in the path
            if (System.IO.File.Exists(targetPath))
            {
                //How to change the file name
                for (int a = 1; a < 100000; a++)
                {
                    fileName = string.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(sourceName), a, Path.GetExtension(sourceName));//If you add 10000 digital suffixes, there is still a file with the same name. I recognize it. Overwrite it. I'm tired
                    targetPath = Path.Combine(folderPath, fileName);
                    //Jump out when the same name does not exist
                    if (!System.IO.File.Exists(targetPath))
                        break;
                }
            }
            FileInfo file = new FileInfo(sourceName);
            file.CopyTo(targetPath, true);//Copy source to targetPath
            //file.MoveTo(targetPath);// Cut source to targetpath
                }

Call the function to cut or copy
Add the following code at the end of the click event of button2:

  while (listBox1.Items.Count > 0)
            {
                string source = listBox1.Items[0].ToString();              
                CopyToFileTianhouzhui(@source, textBox2.Text );
                listBox1.Items.Remove(listBox1.Items[0]);
                label1.Text = listBox1.Items.Count.ToString();
            }

Complete the original destination

use

After running the program, click button1 and select the path to save in the saveFileDialog. After confirmation, the initial directory of traversal will appear in textbox1 and the paths of all files traversed will appear in listbox,
Click button2 and select the path to save the file in saveFileDIalog. After confirmation, the path to save the file appears in textbox2. At the same time, paste or cut it according to different copy functions, and the path in listbox is continuously removed.

Simple extension idea

1. Display the number of paths in listbox in real time,
2. Paste or cut can be selected
3. You can make certain constraints on the suffix of the traversed file
4 set the reaction of button 2 in case of accidental touch without jamming the program.
5. Add, delete and clear all listbox.
5 interface optimization
Note: the copy file function and traversal function in this example are from others in CSDN. I'm sorry that the exact source is not recorded. Sorry.

Topics: C#