Link to home
Start Free TrialLog in
Avatar of wademi
wademi

asked on

Help with C# Input/Output

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)
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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.
Avatar of wademi
wademi

ASKER

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
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

Avatar of wademi

ASKER

wdosanjos:
Thanks. That work. Can you show me an example of how to write the results to a file.
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial