0

i have a window form in which display all directories in listview .for copying files if a file already exist its copies it again appending file.txt(1) with it.and if this is again copied file.txt(2)

string fileNameOnly = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file);
string pathDir = Path.GetDirectoryName(file);
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
string newfileName = Path.Combine(pathDir, tempFileName + extension);

if (MessageBox.Show(file + "is already exists\r\nDo you want to copy Again?",
                    "Overwrite", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    //  Directory.Move(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    File.Copy(file, Path.Combine(new string[] { DestinationFolder, newfileName }));
    MessageBox.Show("File Copied");

but the problem is this that when i copy the files again and again the pattern is like file.txt(1)/file.txt(1)(1)/file.txt(1)(1)(1)/ it doesnt increment the number inside ..i dont know how to increment the count every time i copy..can anybody tell what im doing wrong

2
  • 2
    I've fixed up your code formatting this time, but in future please tidy up your question before posting it...
    – Jon Skeet
    Commented Jan 25, 2013 at 15:18
  • 1
    (darn it... Jon Skeet just beat my edit by a mere second. He even edits fast!)
    – abelenky
    Commented Jan 25, 2013 at 15:18

3 Answers 3

1

count++ means that the variable gets incremented after it's evaluated. You probably want this instead:

string tempFileName = string.Format("{0}({1})", fileNameOnly, ++count);

(note that i've also moved the paranthesis outside of the braces)

Edit:

Also, you're always using the complete file-name(without extension) to create the new one. But you should replace the old-number with old-number+1.

Try this instead:

int count = 0;
string fullPath = file;
while (File.Exists(fullPath))
{
    string fileName = Path.GetFileName(file);
    string extension = Path.GetExtension(fileName);
    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
    int lastIndexOfOpenBracket = fileNameWithoutExtension.LastIndexOf('(');
    string fileNameWithoutNumber = fileNameWithoutExtension.Substring(0, lastIndexOfOpenBracket);
    fileNameWithoutExtension = string.Format("{0}({1})", fileNameWithoutNumber, ++count);
    fullPath = Path.Combine(DestinationFolder, fileNameWithoutExtension + extension);
}
if (MessageBox.Show(file + " already exists\r\nDo you want to copy Again?",
        "Overwrite", MessageBoxButtons.OKCancel,
        MessageBoxIcon.Asterisk) == DialogResult.OK)
{
    File.Copy(file, fullPath);
    MessageBox.Show("File Copied");
}
5
  • it displays file.txt(1)(2)(2) when i copy it third time
    – engineer41
    Commented Jan 25, 2013 at 15:41
  • @engineer41: Have you really used the complete code? I've changed all. file is the full-path to the file? Commented Jan 25, 2013 at 16:02
  • @engineer41: Try again. I've forgotten to replace the old (1) part with the new. Have a look at the lastIndexOfOpenBracket part. Commented Jan 25, 2013 at 16:21
  • ys but still we r not checking the condition that if the ( conatain 1 at next index then increment it to 2 as if its 2 increment it to 3..what we r incrementing is (1)....n after 2nd edit it do like this...f.txt(1)....f.txt(1)(2)....f.txt(1)(1)(2)
    – engineer41
    Commented Jan 25, 2013 at 16:36
  • let us continue this discussion in chat Commented Jan 25, 2013 at 16:41
1

I think following should work for you.

string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);

Anything inside {} is a placeholder. you have used {(1)} which will always be 1.

1
0

you have done simple mistake

string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);

replace {(1)} with ({1})

Not the answer you're looking for? Browse other questions tagged or ask your own question.