none
WinForms for search and move files RRS feed

  • Question

  • Hello,
    I need to write a WinForms application where a user can insert some code (
    in a listbox or a large textbox with values separated by semicolon, or 
    something else) and the application search within a folder if exist files that 
    has these string as part of the file name). 
    The founded files, then, should be moved to another directory.
    How can I solve this problem?

    Thanks a lot in advance. 

    Luigi
    Wednesday, September 18, 2019 6:03 PM

Answers

  • Hi Ciupaz,

    To achieve this requirement, you can refer to the following steps.

    First, use method "String.Split()" to get the specified string.

        string[] p = textBox1.Text.ToString().Split(';');

    Then use Directory.GetFiles to return the names of files that meet specified criteria. And call method File.Move() to move a specified file to a new location.

        foreach (string i in p)
        {
            //Search string to match file name in path
            string searchPattern = "*" + i + "*";
            //search the files
            String[] afi = Directory.GetFiles(source, searchPattern);
            foreach (string file in afi)
            {
                String fName = new FileInfo(file).Name;
                // Move the file
                File.Move(file, destination + new FileInfo(file).Name);
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 7:48 AM

All replies

  • Show us what code you already have.  The problem as you have described it is easy.  You can use Directory.GetFiles to get the files within a directory (and it even has an option to look for patterns).  After that, System.IO.File.Move will move then to a different directory.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, September 19, 2019 12:31 AM
  • At the moment I don't have anything. 

    Do you have some examples?

    Take in consideration that I have more strings to search for, so for each of them, I have to search a file that contains that string in the file name and, if found, move it to another folder. 

    Luigi

    Thursday, September 19, 2019 7:07 AM
  • Hi Ciupaz,

    To achieve this requirement, you can refer to the following steps.

    First, use method "String.Split()" to get the specified string.

        string[] p = textBox1.Text.ToString().Split(';');

    Then use Directory.GetFiles to return the names of files that meet specified criteria. And call method File.Move() to move a specified file to a new location.

        foreach (string i in p)
        {
            //Search string to match file name in path
            string searchPattern = "*" + i + "*";
            //search the files
            String[] afi = Directory.GetFiles(source, searchPattern);
            foreach (string file in afi)
            {
                String fName = new FileInfo(file).Name;
                // Move the file
                File.Move(file, destination + new FileInfo(file).Name);
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 7:48 AM
  • Thank you Kyle, that could be fine. 

    Luigi

    Thursday, September 19, 2019 8:17 AM