Skip to content

Instantly share code, notes, and snippets.

@stuartd
Created June 11, 2019 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartd/7e15b2a22c853dc56f8f75924bfd78c4 to your computer and use it in GitHub Desktop.
Save stuartd/7e15b2a22c853dc56f8f75924bfd78c4 to your computer and use it in GitHub Desktop.
var name = "directory name";
var path = @"c:\temp\dirtest";
for (int i = 0; i < 12; i++)
{
CreateDirectory(name, path);
}
void CreateDirectory(string directoryName, string filePath)
{
var basePath = Path.Combine(filePath, directoryName);
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
return;
}
/*
"directory name"
"directory name (1)"
"directory name (2)"
etc
*/
var matches = Directory.GetDirectories(path, $"{directoryName} (*)");
Directory.CreateDirectory(Path.Combine(filePath, directoryName + $" ({matches.Length + 1})"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment