?
Solved

Help with C# Input/Output

Posted on 2011-03-21
5
Medium Priority
?
327 Views
Last Modified: 2013-12-17
I need help to create a console applicatation that accepts the path to the
location of a folder(Example FolderA). The application should  search every single subfolder in FolderA that are missing a particular file type and output the name of those folders. So  FolderA may have Folder1,Folder2,Folder3 as sub folders. If For example Folder2 and Folder3 doesnt have a .JPG file the program would output a report to a text file.

For exapmple "Folder2: Missing: .jpg"
             "Folder3: Missing: .jpg"

So the console application would accept the path to FolderA and the file types we are checking for that are missing.

Example:C:\Documents and Settings\pazosb>Checkfile.exe 'C:\FolderA' (.jpg,.mpeg)
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
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35185061
Try this:

public static void Main(String[] args)
{
	if (args.Length != 2)
	{
		Console.WriteLine("Invalid parameters");
		return;
	}
	
	var extensions = Regex.Matches(args[1], @"\.\w+");
	
	foreach (var 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);
			}
		}
	}
	
}

Open in new window


I hope this helps.
0
 

Author Comment

by:wademi
ID: 35185378
Hi wdosanjos:
I am Getting these errors when I build the solution. I addes system.io

Error      1      The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\Wademi\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs      19      13      ConsoleApplication1
Error      2      The name 'Regex' does not exist in the current context      C:\Documents and Settings\Wademi\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs      19      30      ConsoleApplication1
Error      3      The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\Wademi\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs      21      22      ConsoleApplication1
Error      4      foreach statement cannot operate on variables of type 'var' because 'var' does not contain a public definition for 'GetEnumerator'      C:\Documents and Settings\Wademi\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs      23      17      ConsoleApplication1
Error      5      The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\Wademi\My Documents\Visual Studio 2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs      23      26      ConsoleApplication1
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35185429
Try this:

using System.Text.RegularExpressions;
using System.IO;

public static void Main(String[] args)
{
	if (args.Length != 2)
	{
		Console.WriteLine("Invalid parameters");
		return;
	}
	
	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);
			}
		}
	}
	
}

Open in new window

0
 

Author Comment

by:wademi
ID: 35185494
wdosanjos:
Thanks. That work. Can you show me an example of how to write the results to a file.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35185828
Sure. There you go.

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);
				}
			}
		}
	}
}

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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