Skip to content

Instantly share code, notes, and snippets.

@hanssens
Last active November 18, 2015 22:59
Show Gist options
  • Save hanssens/9a43f61e63a3782a72e8 to your computer and use it in GitHub Desktop.
Save hanssens/9a43f61e63a3782a72e8 to your computer and use it in GitHub Desktop.
ScriptCS script that extracts all unique file extensions from a directory
using System.IO;
// extracts all file extensions from a specified directory
// and spits them out in the console
var path = Directory.GetCurrentDirectory();
var root_path = new DirectoryInfo(path);
// this fetches all file info, extracts the extension,
// filters the unique ones and sorts them, before outputting it
// alternatively, you may use 'SearchOption.AllDirectories' for recursive search
var extensions = root_path.GetFiles("*.*", SearchOption.TopDirectoryOnly)
.Select(f => f.Extension)
.Distinct()
.OrderBy(f => f)
.ToList();
Console.WriteLine(string.Join(Environment.NewLine, extensions));
/*
# sample output
$ scriptcs foo.csx
.csx
.DS_Store
.txt
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment