Solved

Help with C# Input/Output

Posted on 2011-03-21
5
322 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
  • 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

707 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