| |
Search Site/Articles
Archived Articles
712 Articles
C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal
|
|
| Printable Version
Project Line Counter in C#
By Manuel Lucas Vinas Livschitz
A project line counter with recursion. Made in 5 minutes.
using System;
using System.Collections;
using System.IO;
namespace LineCounter
{
/// <summary>
/// Console App.
/// </summary>
class App
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Line counter. Copyright 2004 - M.L. Viqas Livschitz");
string argument="";
if(args.Length==0)
{
Console.WriteLine("One target folder needed as argument. (a path with space characters requires double-quotes).");
return;
}
if(args[0].StartsWith("\""))
{
argument=args[0].Substring(1);
for(int j=1;j<args.Length;j++)
{
if(args[j].EndsWith("\""))
{
argument+=" "+args[j].Substring(0,args[j].Length-1);
break;
}
else
argument+=" "+args[j];
}
}
else
{
argument=args[0];
}
SortedList htfiles = new SortedList();
Stack PendingFolders = new Stack();
PendingFolders.Push( argument );
while(PendingFolders.Count>0)
{
string pf = PendingFolders.Pop() as string;
string[] files = System.IO.Directory.GetFiles(pf);
foreach(string filename in files)
{
string ext = Path.GetExtension(filename.ToLower());
if( (ext!=".txt") &&(ext!=".java") &&(ext!=".cs")
&&(ext!=".resx") &&(ext!=".h") &&(ext!=".c") &&(ext!=".cpp")
&&(ext!=".res") &&(ext!=".nsi") &&(ext!=".xml") &&(ext!=".html") &&(ext!=".htm") &&(ext!=".jsp")) continue;
if(ext==".c") ext = "C/C++ code";
if(ext==".cpp") ext = "C/C++ code";
if(ext==".h") ext = "C/C++ code";
if(ext==".res") ext = "C/C++ code";
if(ext==".cs") ext = "C# code";
if(ext==".resx") ext = "C# code";
if(ext==".java") ext = "Java/JSP code";
if(ext==".jsp") ext = "Java/JSP code";
if(ext==".html") ext = "HTML/XML code";
if(ext==".htm") ext = "HTML/XML code";
if(ext==".xml") ext = "HTML/XML code";
if(ext==".nsi") ext = "Install script code";
if(!htfiles.ContainsKey(ext))
htfiles[ext]=0;
StreamReader str = new StreamReader(filename);
string buf; int l=0;
while( null!=(buf=str.ReadLine()) )
{
l++;
}
htfiles[ext] = (int)htfiles[ext] + l;
str.Close();
}
string[] subfolders = System.IO.Directory.GetDirectories(pf);
foreach(string s in subfolders)
{
PendingFolders.Push(s);
}
}
foreach(string ext in htfiles.Keys)
{
Console.WriteLine("Number of lines for file type \t"+ext+": \t"+((int)htfiles[ext]).ToString());
}
Console.ReadLine();
}
}
}
|
|