我有許多文件夾,每個文件夾中有超過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++;
}
}
}