1

I am generating some files (simplified version)

private static void GenFiles(int numbersToGenerate, string directory)
{
    for (int i = 1; i <= numbersToGenerate; i++)
    {
        string fileName = Path.Combine(directory, "File" + i);
        File.Create(fileName);
    }
}

Now i am told "Generate 100 files and Create 10 Folders(Folder1-Folder2-Folder3 etc..)place 10 items in each folder"

Would you create all the files in a directory and then create each folder move files to folder till no file left ?

private static void MoveToFolders(string targetDirectory,int numberOfFolders, int numberOfFilesGenerated)
{
    int itemsPerFolder = numberOfFilesGenerated / numberOfFolders;
    ?????
    //
}

Suggestions?

Naeem Sarfraz
  • 7,360
  • 5
  • 37
  • 63
user9969
  • 15,632
  • 39
  • 107
  • 175
  • 6
    Homework? This sounds like a test of your ability to translate requirements into a working product. If so, answering this would be doing you a disservice.This skill is a must-have in the real world, and your best way to develop it is via experience and trial and error. If this isn't homework, I apologize for assuming so. – David Nov 19 '11 at 07:58
  • @DavidStratton it's not homework at all.I need to move /organize thousands of files and just wondering what the best /fastest approach would be.A noddy example would be great.My real world scenario is much more complex,i have learnt that when posting a questions the simpler it is the better.I dont want to confuse by posting a convoluted problem. – user9969 Nov 19 '11 at 08:11
  • Then I would throw my support to @Tigran's answer. A nested loop on file creation is how I'd do it. – David Nov 19 '11 at 08:12
  • http://stackoverflow.com/questions/2955402/how-do-i-create-directory-if-it-doesnt-exist-to-create-a-file – regisbsb Aug 24 '15 at 08:21

1 Answers1

3

I would say during the loop creare folder and create a file inside. Less steps to achieve your goal.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Is it a case of keep counting the folders and moving stuff inside. Do you think it would be faster than generating all the files in one folder and then moving them to other folders.Simpler in a way but more inefficient?# – user9969 Nov 19 '11 at 08:19
  • To me it seems faster as requires less steps to achieve the required final state. In case you create and after move, IMHO, you should technically do all what requires in first option + file move. But I suggest to measure, especially if you are talking about thousands of files. – Tigran Nov 19 '11 at 08:24