Skip to content

Instantly share code, notes, and snippets.

@LindaLawton
Last active Apr 20, 2018
Embed
What would you like to do?
Console application for scanning files
using System;
using System.IO;
using System.Linq;
namespace FileScanner
{
public class Program
{
private const string _locationOfRepoToScan = @"C:\Development\IdentiyServer\IdentityServer4\src\IdentityServer4";
private const string _localDirectoryReplace = @"C:\Development\IdentiyServer\IdentityServer4\";
private const string _gitHubRepoLocationReaplace = "https://github.com/LindaLawton/IdentityServer4/blob/GDPR/";
private const int _gitLineNumberOffset = 1;
private static string _mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private static string _outputFile = _mydocpath + @"\IdentityServerGDPRScann.txt";
private const string _SearchFor = "_logger.";
private const string _InFilesOfType = ".cs";
public static void Main(string[] args)
{
if (File.Exists(_outputFile))
{
File.Delete(_outputFile);
}
if (Directory.Exists(_locationOfRepoToScan))
{
ProcessDirectory(_locationOfRepoToScan);
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
var fileEntries = Directory.GetFiles(targetDirectory).Where(a => Path.GetExtension(a) == _InFilesOfType).ToList();
foreach (var fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
var subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (var subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
public static void ProcessFile(string path)
{
using (var outputFile = new StreamWriter(_outputFile, true))
{
var lines = File.ReadLines(path)
.Select((line, lineNumber) => new { LineNumber = lineNumber, Line = line })
.Where(line => line.Line.Contains(_SearchFor))
.ToList();
if (lines.Count <= 0) return;
outputFile.Output("\r\n");
outputFile.Output($"[{Path.GetFileName(path)}]({BuildGitHubLink(path)})");
foreach (var line in lines)
{
outputFile.Output($"{line.LineNumber + _gitLineNumberOffset}. `{line.Line.Trim()}` [{Path.GetFileName(path)}]({BuildGitHubLink(path)}#L{line.LineNumber + _gitLineNumberOffset})");
}
}
}
public static string BuildGitHubLink(string path)
{
return path.Replace(_localDirectoryReplace, _gitHubRepoLocationReaplace).Replace("\\", "/");
}
}
public static class StreamWriterExtensions
{
public static void Output(this StreamWriter stream, string line)
{
Console.WriteLine(line); // I like to watch while debuging.
stream.WriteLine(line);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment