Solved

C# control stucture

Posted on 2011-03-22
2
216 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
  • 2
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
Comment Utility
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 29

Expert Comment

by:anarki_jimbel
Comment Utility
BTW, I didn't test...
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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