This article describes my first program in C# that deals with a folder and its files. This program is useful to those that suffer from a large number of folders in their desktops or volumes and want to arrange their folders with specific names and move them from one location to another.
The program divides the folders into a wanted folder or path according to a number that the user will enter. If a user want to move a11 folders from folder A to folder B and wants to put two folders in a new created folder in B, with a new name, this program will do that easily !
The mover program needs some inputs to work properly, which are:
- The divide number.
- The name of new created folder (Optional).
- Source folder or path.
- Destination folder or path.
My program counts the number of folders in the source folder, puts the result in x and checks that the divide number is less than x. Dividing x by the entered number and ceiling the result will give us the least number of folders needed to be created, and contain the folders to be transfered from the source.
The program name for the new created folder (which will host the folder to be transfered) with the entered name as inputted by the user. If the user wants to not enter this field, the program will name the folder with the mover word. In both cases, the program will append a number to the name.
Using the Code
string foldrs = Directory.GetDirectories(input);
if (numberOfFolders > foldrs.Length)
"You can not enter a number that is larger than the number" +
"of folders in source folder !",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
double x = foldrs.GetLength(0);
int index = 0;
for (int i = 0; i < Math.Ceiling(x / numberOfFolders); i++)
runStatus.Text = "Running !";
runStatus.ForeColor = Color.Red;
string folderName = output + "\\" + newFoldersName + " (" + i + ")";
//copying from source into destination
for (int j = 0; j < numberOfFolders; j++)
//the end of source folders, there is no folder of that value of index
if (index >= foldrs.Length) break;
// create the same folder in the destination
// foldrs[index].ToString().LastIndexOf("\\") +
// 1 get the name of each folder in the source folder
string newFolder = folderName + "\\" + foldrs[index].ToString().Substring(
foldrs[index].ToString().LastIndexOf("\\") + 1);
// copying contents
index = index + 1;
//at the end we delete the source folder
I used two methods that do the action of moving (which is copying, then deleting) which is included in the downloadable code at the top of this article.
Points of Interest
The idea is simple but useful and made me start to develop a program to organize and arrange folders and files by type, quantity and number, and displays information about it.
Forgive me for my poor English. It's my first article, my first program, and want it not to be my last.