63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace LineCounter
|
|
{
|
|
class Program
|
|
{
|
|
static int SearchDirectoryAndCountLines(string path, string searchQuery)
|
|
{
|
|
string[] folders = Directory.GetDirectories(path);
|
|
string[] files = Directory.GetFiles(path, searchQuery);
|
|
|
|
int count = 0;
|
|
|
|
foreach (string folder in folders)
|
|
count += SearchDirectoryAndCountLines(folder, searchQuery);
|
|
|
|
foreach (string file in files)
|
|
count += File.ReadAllLines(file).Length;
|
|
|
|
return count;
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
string SearchQuery = GetSearchQuery();
|
|
|
|
if (args.Length == 0)
|
|
{
|
|
Console.WriteLine("No Arguments found, drop a folder to the executable");
|
|
Console.ReadLine();
|
|
return;
|
|
}
|
|
|
|
Console.WriteLine("Started counting...");
|
|
|
|
string path = args[0];
|
|
int lineCounts = SearchDirectoryAndCountLines(path, SearchQuery);
|
|
|
|
Console.WriteLine($"{ path } has { lineCounts } lines with the search query of \"{ SearchQuery }\"");
|
|
Console.ReadLine();
|
|
}
|
|
|
|
private static string GetSearchQuery()
|
|
{
|
|
const string defaultFileType = "*.cs";
|
|
|
|
string searchQueryPath = Path.Combine(System.AppContext.BaseDirectory, "SearchQuery.txt");
|
|
|
|
if (File.Exists(searchQueryPath))
|
|
return File.ReadAllText(searchQueryPath);
|
|
|
|
Console.WriteLine($"The search query file { searchQueryPath } was not found. The file is created at the executable folder with current query of \"{ defaultFileType }\"");
|
|
FileStream fileStream = File.Create(searchQueryPath);
|
|
fileStream.Write(Encoding.UTF8.GetBytes(defaultFileType));
|
|
fileStream.Close();
|
|
|
|
return defaultFileType;
|
|
}
|
|
}
|
|
}
|