Solved

Help with c# solution

Posted on 2011-03-21
4
267 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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