Solved

Help with c# solution

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now