Solved

Help with c# solution

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

Suggested Solutions

Title # Comments Views Activity
Problems running a .Net executable from Task Scheduler, program uses Process.Start 7 27
Syntax error 9 45
Run time Error 4 34
Help with C#, MVC, razor. 6 23
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

910 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

20 Experts available now in Live!

Get 1:1 Help Now