Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Let us consider,

E:/Folder2/Fol24/Fo2222
E:/Folder3/Fol23/Folder333
E:/Folder4/Folde25/Fold44
E:/Folder5/Folder55/Folde5

I need to archive and remove all the files from wildcard character matching folders using some input like E:/Folder?/Fol2?/Fo*
Here I am expecting the folders E:/Folder2/Fol24/Fo2222 and E:/Folder3/Fol23/Folder333

The problem here is the user can enter any pattern of wildcard characters. According to the pattern, I have to copy the files to other location and remove the files. Here I can't use any fixed search pattern. It should be dynamic and can have any no. of wildcard characters.

Thanks for your help.

Regards
Chandra
Posted
Updated 29-Dec-15 2:50am
v3
Comments
Nathan Minier 29-Dec-15 7:26am    
Smells like homework.

Please have a look at the handy foreach() flow construct and recursive function concepts.
BillWoodruff 29-Dec-15 8:03am    
You posted a very similar question here on December 18th. this year:

http://www.codeproject.com/Questions/1065430/How-do-I-Copy-the-files-from-folders-recursively-w

I and other people took their time to respond and comment, and provide solutions.

You did not vote once, or respond to any comments.

Why would we want to help you now ?
ChandraJ 29-Dec-15 23:34pm    
@BillWoodruff, I am sorry. As I needed to close the current task immediately, I have concentrated on the solution only. So couldn't think in that way.
ChandraJ 29-Dec-15 23:37pm    
I have badly struck in this area.

Directory.GetFiles(string, string) allows you to specify a search pattern. Directory.GetFiles Method (String, String) (System.IO)[^]
 
Share this answer
 
Comments
BillWoodruff 29-Dec-15 9:22am    
Hi Ryan, fyi: the Op has had how wildcard search works with the System.IO "get" File/Directory operators explained in detail before:

http://www.codeproject.com/Questions/1065430/How-do-I-Copy-the-files-from-folders-recursively-w
ZurdoDev 29-Dec-15 9:35am    
Interesting. I did see your comment; I just didn't follow your link. Interesting that OP doesn't ever respond.
ChandraJ 29-Dec-15 23:35pm    
@BillWoodruff, RyanDev: I am sorry. As I needed to close the current task immediately, I have concentrated on the solution only. So couldn't think in that way.
ZurdoDev 30-Dec-15 7:14am    
Not a problem. Just wondering why you asked twice.
ChandraJ 31-Dec-15 1:14am    
Hi RyanDev, task is not yet finished. Still I am struggling for closure for the above pattern which I mentioned in the latest question.
C#
private static List<string> folders = new List<string>();

    private void buttonExample_Click(object sender, EventArgs e)
    {
        folders.Clear();

        string input = textBox1.Text;
        string currentDir = string.Empty, currentPattern = string.Empty, nextPattern = string.Empty;

        foreach (char c in input)
        {
            if (c == '?')
            {

                string[] f = input.Split(new[] {'?'}, 2);
                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }

                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('?'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
            else if(c == '*')
            {
                string[] f = input.Split(new[] { '*' }, 2);

                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }
                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('*'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
        }
        DirectoryInfo di = new DirectoryInfo(currentDir);
        List<string> foldersNew = new List<string>();
        foldersNew = GetAllFolders(di, currentPattern, nextPattern);
    }

    private static List<string> GetAllFolders(DirectoryInfo currentDir,string currentPattern, string nextPatten)
    {
        DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);
        if (dis.Length == 0 && string.Equals(currentPattern,string.Empty))
            folders.Add(currentDir.FullName);

        if (dis.Length > 0)
        {
            string[] remainPattern = nextPatten.Split("\\".ToCharArray());
            if (remainPattern.Length > 0)
            {
                foreach (DirectoryInfo di in dis)
                {
                    GetAllFolders(di, remainPattern.First(),
                                   string.Join("\\", remainPattern.Skip(1).ToArray()));
                }
            }

        }

        return folders;
    }


Quote:
My solution might be helpful for those who need. This will solve the problem. Thanks for all for your help.

Reference taken from: http://stackoverflow.com/questions/3306445/parsing-an-canonical-path-with-wildcards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900