私はそれぞれに500,000以上のアイテムを持つ多数のフォルダーを持っています。私は10,000(または50,000または5,000またはユーザーが定義しているもの)のフォルダにこれらを分割したい。フォルダーごとにx個のファイルを含むフォルダー構造を作成する
明らかに私の論理には何か間違っている。現在のところ、すべてのファイルを最初のフォルダに移動するだけなので作成します。私はforeachと場所の異なる組み合わせで遊んでみましたが、運はありません。
//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++;
}
}
}