Skip to content

Instantly share code, notes, and snippets.

@relliv
Created February 22, 2019 08:32
Show Gist options
  • Save relliv/cac4d9ef2ee238c8941a87a682ae8b82 to your computer and use it in GitHub Desktop.
Save relliv/cac4d9ef2ee238c8941a87a682ae8b82 to your computer and use it in GitHub Desktop.
C# Auto Numeric Folder Creator With LINQ
private bool AutoFolderCreate(int incrementCount = 1)
{
var targetPath = Directory.GetCurrentDirectory() + "\\Saves";
if (Directory.Exists(targetPath))
{
var newDirName = Directory.EnumerateDirectories(targetPath) // list of sub directories in target
.Select(Path.GetFileName) // get directory/file name
.Where(n => n.All(Char.IsDigit)) // get only digits
.Select(int.Parse) // string parse to int
.Max() // get biggest number
+ 1; // add 1
if (!Directory.Exists($"{targetPath}\\{newDirName}"))
{
Directory.CreateDirectory($"{targetPath}\\{newDirName}");
return true;
}
}
return false;
}
@relliv
Copy link
Author

relliv commented Feb 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment