?
Solved

C# control stucture

Posted on 2011-03-22
2
Medium Priority
?
231 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 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