我有许多文件夹,每个文件夹中有超过500,000个项目。我想把它们分解成10000个文件夹(或50,000或5,000个文件夹或任何用户定义的文件夹)。为每个文件夹创建x个文件夹的文件夹结构
在我的逻辑中显然存在一些问题,因为它现在只是将所有文件移动到第一个文件夹中创建。我尝试过使用不同的foreach和where组合,但没有运气。
//Find all the files to move
string[] files = Directory.GetFiles(textBox1.Text, "*.*", SearchOption.TopDirectoryOnly);
//Use selects the number of files to go in each folder
long h = long.Parse(tbFilePerFolder.Text);
//Used later
long i = 0;
//Used later
long j = 0;
//Get the number of folders to create
long k = files.Count()/h;
//Report back the number of files found
lblFilesFound.Text = "Files Found: " + files.Count();
//Create the necessary number of folders, plus 1 to pick up remainders
while (j <= k + 1)
{
Directory.CreateDirectory(textBox1.Text + @"\" + j.ToString("00000"));
lblFoldersCreated.Text = "Folders Created: " + j;
j++;
}
//Get each folder that's just been created
string[] folders = Directory.GetDirectories(textBox1.Text, "*.*", SearchOption.TopDirectoryOnly);
//For each of those folders...
foreach (string folder in folders)
{
//While there is less than the requested number of folders...
while (i <= h)
{
//Get a list of the currently existing files
string[] files2 = Directory.GetFiles(textBox1.Text, "*.*", SearchOption.TopDirectoryOnly);
//And iterate through it, moving to the defined directory
foreach (string file in files2)
{
File.Move(file, folder + @"\" + Path.GetFileName(file));
lblFilesMoved.Text = "Files Moved: " + i;
i++;
}
}
}