Reputation: 111
I have tried how to make auto increment folder, but there is a problem when numbering, does anyone know where is the problem?
public void NewFolder()
{
try
{
string FolderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
tempFolder = FolderName;
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
else if (Directory.Exists(tempFolder))
{
tempFolder = tempFolder + ("001");
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
else if (Directory.Exists(tempFolder))
{
int x = 1;
for (x = 0; x < 50; x++)
{
string angkaString = tempFolder.Substring(tempFolder.Length - 3);
int angka = Convert.ToInt32(angkaString) + x;
string angka00 = "00" + angka.ToString();
tempFolder = FolderName + angka00.Substring(angka00.Length - 3);
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
return;
}
}
}
}
MessageBox.Show(tempFolder);
}
catch (IOException ex)
{
MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Here is output the name folder = folder001,folder002,folder004,folder007,folder11,folder16,folder22.
I want make the name = folder001,folder002,folder003,folder004.. next
Upvotes: 0
Views: 291
Reputation: 54877
I've cleaned up your code to get the functionality you're after:
public void NewFolder()
{
try
{
string folderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
else
{
for (int x = 1; x < 50; x++)
{
string tempFolder = folderName + x.ToString().PadLeft(3, '0');
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
MessageBox.Show(tempFolder);
break;
}
}
}
}
catch (IOException ex)
{
MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Upvotes: 2
Reputation: 2806
You code style is not very clear. You should use good names for you methods, variables etc. Naming is very important.
A good solution is to use string formatting provided by the .ToString()
method.
I fixed up and simplified your code:
public void NewFolder()
{
try
{
string folderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
else
{
// Creates 50 folder from 001 up to 050
// This works also if we have folders from 001 up 022 allready. It will extend up to 050
for (int i = 1; i <= 50; i++)
{
// formatting the string for 3 digits like 001, 002 and so on.
string angka00 = i.ToString("D3");
var folderWithNumber = folderName + angka00;
if (!Directory.Exists(folderWithNumber))
{
Directory.CreateDirectory(folderWithNumber);
}
}
}
}
catch (IOException ex)
{
MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Upvotes: -2
Reputation: 1658
Try this..
int angka = Convert.ToInt32(angkaString) + x; instead of adding x here you can add 1 directly
public void NewFolder()
{
try
{
string FolderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
tempFolder = FolderName;
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
else if (Directory.Exists(tempFolder))
{
tempFolder = tempFolder + ("001");
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
else if (Directory.Exists(tempFolder))
{
int x = 1;
for (x = 0; x < 50; x++)
{
string angkaString = tempFolder.Substring(tempFolder.Length - 3);
int angka = Convert.ToInt32(angkaString) + 1;
string angka00 = "00" + angka.ToString();
tempFolder = FolderName + angka00.Substring(angka00.Length - 3);
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
return;
}
}
}
}
MessageBox.Show(tempFolder);
}
catch (IOException ex)
{
MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Upvotes: 1
Reputation:
Your problem is here:
int angka = Convert.ToInt32(angkaString) + x;
Use this:
int angka = Convert.ToInt32(angkaString) + 1;
Upvotes: 2