I'm trying to select certain amount of files randomly from a folder. For example if a folder has 100 files, I want to select 40 files randomly rather than the first 40.
string sourceFolder = //unc path;
var dir = new DirectoryInfo(sourceFolder );
var allFiles = dir.GetFiles("*.pdf");
int fileCount = allFiles.Length; // 100 files
int folderOne = 60;
int folderTwo = 40;
if (fileCount > 0)
{
// select 60 files randomly and move them to folderOne
}
I tried using Random function in C# but I cannot get my head around it.
var random = new Random();
int index = random.Next(0, fileCount - 1);
var file = allFiles[index].FullName;
Any help would be greatly appreciated. Thanks
Randomize the order of the files, then you can simply copy the first 60 to one location, and the last forty somewhere else.
eg