A simple and naive C# program that will help identify binary file types in a directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Recommended usage is to pipe in a list of paths : | |
* Git: | |
* git ls-files | FindBinaryFiles.exe | |
* PowerShell: | |
* ls -r | ? PSIsContainer | select -expand FullName | FindBinaryFiles.exe | |
* */ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
class Program | |
{ | |
static void Main() | |
{ | |
var files = GetFileListFromStdin() | |
.Select(f => f.ToUpperInvariant()) | |
.Where(NotExcluded) | |
.GroupBy(Path.GetExtension) | |
.OrderBy(g => g.Key) | |
.Where(g => g.Key.Length != 0); | |
Console.WriteLine("Binary files:"); | |
foreach (var extn in files) | |
{ | |
if (extn.Any(IsBinaryFile)) | |
{ | |
Console.WriteLine(extn.Key); | |
} | |
} | |
} | |
static IEnumerable<string> GetFileListFromStdin() | |
{ | |
string s; | |
while ((s = Console.ReadLine()) != null) | |
{ | |
yield return s; | |
} | |
} | |
static bool NotExcluded(string fullPath) | |
{ | |
var exclude = new[] { @"\\ThirdParty\\", @"\\bower_components\\", @"\\node_modules\\" }; | |
return !exclude.Any(ex => Regex.IsMatch(fullPath, ex, RegexOptions.IgnoreCase)); | |
} | |
static bool IsBinaryFile(string fullPath) | |
{ | |
using (var file = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 10240, FileOptions.SequentialScan)) | |
{ | |
while (true) | |
{ | |
int currentByte = file.ReadByte(); | |
if (currentByte == -1) | |
return false; | |
if (currentByte == 0) | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment