I ve managed to get this far on my first C# app. The problem is when I am adding files and creating a sub directory to a already existing folder it works but when I re-do the action it won't create a new sub directory as the previous one, as is has the same name to that of the new one. I have tried incrementing the folder number to that of the previous folder name but this doesn't work. I know I could when a new sub directory is created to store it as a variable and then loop through to find if that variable is existing, if it's not then create a new one. Any ideas? This is my last bit to do then I have finished it! Yes!
Thanks.
private void butSort1_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(@"C:\new1");
FileInfo[] fiArr = di.GetFiles("*.txt");
int iCtr = 0;
int iloop = 0;
try
{
foreach (FileInfo fri in fiArr)
{
//create a new directory
DirectoryInfo dir = new DirectoryInfo(@"C:\new2");
//If that directory doesn't exsit create a new directory
if (!dir.Exists)
dir.Create();
DirectoryInfo dis = dir.CreateSubdirectory("newnew" + "(" + iloop + ")");
if (!dis.Exists)
{
iloop = +1;
dis.Create();
}
// Move files from dir to the new sub directory
//(fri.FullName.ToString(), "File Manager");
fri.MoveTo(Path.Combine(dis.FullName, iCtr + ".txt"));
//loop round to add the file name
iCtr++;
//Refresh the label attributes
RefreshDirSize1();
RefreshDirSize2();
RefreshDirSize3();
RefreshDirSize4();
}
}
catch (Exception b)
{
lblErrors.Text = (b.ToString());
}
}