Created
February 22, 2019 08:32
-
-
Save relliv/cac4d9ef2ee238c8941a87a682ae8b82 to your computer and use it in GitHub Desktop.
C# Auto Numeric Folder Creator With LINQ
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source