Solved

Help with C# Input/Output

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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