Solved

Help with c# solution

Posted on 2011-03-21
4
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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