Solved

C# control stucture

Posted on 2011-03-22
2
223 Views
Last Modified: 2013-12-17
I am using the following code to check the contents of a fodler then output the name of the folder missing specific file types.Example(.mov,.mpeg)

Its a console application. So I may run the following  from cmd.
C:\Consoleapplication1.exe FolderX (.mov,.mpeg)


Here is the code

public static void Main(String[] args)
{
      if (args.Length != 2)
      {
            Console.WriteLine("Invalid parameters");
            return;
      }
      
      MatchCollection extensions = Regex.Matches(args[1], @"\.\w+");
      
      using (StreamWriter writer = new StreamWriter("MyOutputFile.txt"))
      {
            foreach (string dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories))
            {
                  foreach (Match extension in extensions)
                  {
                        if (Directory.GetFiles(dir, "*" + extension.Value).Length == 0)
                        {
                              writer.WriteLine("{0}: Missing: {1}", dir, extension.Value);
                        }
                  }
            }
      }
}



The problem with the current control is if  it checks for example a directorywith only a  .Mov file  for a .mpeg file first  it outputs the  folder as missing a movie file. This occurs because the .MOv file may be checked last.

I need to ensure that the folders are checked for every file type and only outputs the folder if none of the files in the loop exsist in the folder.

How can I modify my code to achieve the above?
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
  • 2
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35195338
If I properly understand what you want then
try:
using (StreamWriter writer = new StreamWriter("MyOutputFile.txt"))
      {
            foreach (string dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories))
            {
                bool noMoviesInFolder = true;
                foreach (Match extension in extensions)
                {
                     if (Directory.GetFiles(dir, "*" + extension.Value).Length != 0)
                     {
                         noMoviesInFolder = false;
                         break;// we know the folder has movies - so stop
                     }
                }
                if(noMoviesInFolder)
                {
                    writer.WriteLine("No movies in folder " + dir);
                }

            }
      }

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35195340
BTW, I didn't test...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
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!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 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