Search code examples
c#asp.net-mvc-5

how to check if folder name meets requirement


users are supposed to drop folders in a directory

string sourcepath

how to check if all folders and files in the directory have correct format of "numbers space numbers space numbers space" such as "123 456 789" I would thing the pseudocode would be something like this:

  DirectoryInfo parentFolderInfo = new DirectoryInfo(sourcePath);

        foreach (DirectoryInfo folder in parentFolderInfo.GetDirectories())
        {
            if (sourcepath == "correct name")
            {
                FileInfo[] filesInSource = folder.GetFiles();

                foreach (FileInfo file in filesInSource)
                {
                    if (file == "correct name")
                    {
                        //do something 
                    }
                    else
                    {
                        //return box "the folder name is wrong please try again
                    }
                }
            }
            else
            {
                //return box "the folder name is wrong please try again


            }

I think we have to use regular expression for this. thank you


Solution

  • static string pattern = "((.*))([(.*)^\\s])([^\\(_.*)]+)([^\\s]+)";//change the regex ass needed
    static Match result;
    static string formatname = string.Empty;
    DirectoryInfo parentFolderInfo = new DirectoryInfo(sourcePath);
    foreach (DirectoryInfo folder in foldersInSource.GetDirectories())//This loops through all folders from path
      {
        formatname = fileNameVaild.Name;//This gets the folder name 
        result = Regex.Match(fileNameVaild.Name, pattern);//This compares the two values
        if (result.Value == formatname)
           {
            continue;
           }
        else
           {
             return false;//if values != then return false.
           }
       }
    

    I hope this gets someone started.