Solved

Help with C# Input/Output

Posted on 2011-03-21
5
323 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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