Solved

C# control stucture

Posted on 2011-03-22
2
227 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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