Solved

Help with c# solution

Posted on 2011-03-21
4
259 Views
Last Modified: 2013-12-17
Can someone explain the sections of the codes where i have the question marks. I got the following as a solution which worked

    class Program
    {

        public static void Main(String[] args)
        {

           
            if (args.Length != 2)
            {
                Console.WriteLine("Invalid parameters");
                return;
            }

             StreamWriter sw = new StreamWriter(new FileStream(args[0]+@"\myoutput.txt",FileMode.Create,FileAccess.Write));

            ????????????????
            MatchCollection extensions = Regex.Matches(args[1], @"\.\w+");

     
            ?????????????????????????????????????????
            foreach (string dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories))
            {
              ??????????????????????
                foreach (Match extension in extensions)
                {
                    if (Directory.GetFiles(dir, "*" + extension.Value).Length == 0)
                    {
                        Console.WriteLine("{0}: Missing: {1}", dir, extension.Value);
                        sw.WriteLine(DateTime.Now+" "+"{0}: Missing: {1}", dir, extension.Value);
                        sw.Flush();
                    }
                }
            }

        }
    }
}
0
Comment
Question by:wademi
  • 3
4 Comments
 
LVL 7

Expert Comment

by:dimaj
ID: 35186087
Hi!

  MatchCollection extensions = Regex.Matches(args[1], @"\.\w+");
This means that you are creating an object of type MatchCollection which will contain all your matches found by regular expression.

 ?????????????????????????????????????????
            foreach (string dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories))
            {
This is a basic 'for' loop that will loop through all directories found in your args[0]. Every time you would hit this line, dir will contain next directory in the list.

              ??????????????????????
                foreach (Match extension in extensions)
                {
Same as the previous loop, except this loop goes through your matches found in your first questionable line.

Hope this helps.

dimaj
0
 

Author Comment

by:wademi
ID: 35186105
Thanks dimaj. I am more interested in what this part would produce (args[1], @"\.\w+");

so the args from the command line would be consoleapplication1.exe c:\foldera (.jpg)
So the args[1] i think would be (.jpg).

So what does (args[1], @"\.\w+"); do?

Also what does the "*" in  (args[0], "*", SearchOption.AllDirectories)) do?
0
 
LVL 7

Accepted Solution

by:
dimaj earned 500 total points
ID: 35186144
ahh... gotcha

So, based on your command line example, your args will have the following:
args[0] = C:\foldera
args[1] = (.jpg)

(args[1], @"\.\w+") -
@ is used so that you don't have to escape \. If you were to avoid using @, your code would look like:
(args[1], "\\.\\w+").
\.\w+ is a regular expression. See this link for help with regex: http://www.regular-expressions.info/reference.html

The '*' in the  (args[0], "*", SearchOption.AllDirectories) is a search string. It's a wildcard for find all folders. If you have a subforlders subA and subB in your c:\foldera, and you were to change your code to (args[0], "*A", SearchOption.AllDirectories), you would get only subA as a result of that command.

Does that make sense?

dimaj
0
 
LVL 7

Expert Comment

by:dimaj
ID: 35186156
also, so that you won't waste a lot of time researching what that regular expression does, it simply strips all characters that are not part of the extension. \. = match a '.'; \w+ = match a word character until you can't match anymore. I believe this is a non-greedy algorithm.

examples of output:
()()().abcdlkjdfadf()()(* = .abcdlkjdfadf
.jpg = .jpg
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question